react-native-orientation-locker icon indicating copy to clipboard operation
react-native-orientation-locker copied to clipboard

Dimensions not correctly set after locking to portrait

Open austinthetaco opened this issue 6 years ago • 8 comments

Currently I am calling Orientation.lockToPortrait on app load, then for a single component i'm calling lockToLandscape and upon leaving that component again calling lockToPortrait. However when i console log out Dimensions.get('window'); after the last lockToPortrait the width is still larger than the height. Any idea if there is a fix for this?

austinthetaco avatar Jun 06 '19 20:06 austinthetaco

This is a react native problem I think, I've been using View's onLayout method to get the screen width and height

michelegoh avatar Jun 12 '19 06:06 michelegoh

Dimensions.get('window') seems not changed after app started. You can addLockListener(function(orientation)), and use static window width, height (remember when started) to calculate yourself. Eg. s_width = 1080, s_height = 1920 when Portrait, current width = s_width

when Landscape current width = s_height;

wonday avatar Jun 12 '19 10:06 wonday

@wonday It is changed after app start. Upon app launch with lockToPortrait the Dimensions.get('window') returns correct portrait dimensions. Then when i call lockToLandscape Dimensions.get('window') return the correct landscape dimensions. the issue is after this when i call lockToPortrait again but the dimensions don't change.

austinthetaco avatar Jun 13 '19 18:06 austinthetaco

same issue

dellert avatar Jul 07 '19 13:07 dellert

I met this issue only on iOs and got solution, it's a bit tricky, but u can add dimension listener, which set right values

componentDidMount() {
    Dimensions.removeEventListener('change', this.dimensionListener);
  }

componentWillUnmount() {
    Dimensions.removeEventListener('change', this.dimensionListener);
  }

  setDimensionsIos = (dim) => {
    const { screen: { height: screenHeight, width: screenWidth },
      window: { height: windowHeight, width: windowWidth },
    } = dim;

    if (screenHeight === windowWidth && screenWidth === windowHeight) {
      setTimeout(() => Dimensions.set({ 'window': dim.screen }), 0);
    }
  }

  dimensionListener = (args) => {
    if (Platform.OS === 'ios') this.setDimensionsIos(args);
  };

grnkvch avatar Jul 24 '19 09:07 grnkvch

I am encountering the same issue. The incorrect dimensions are causing image resizing problems in our application.

Manually setting the dimensions may work in some cases, but the Dimensions api in react-native core should be the "source of truth" here.

Per the react native Dimensions.set docs:

This should only be called from native code by sending the didUpdateDimensions event.

It would definitely be nice to have this bug fixed in the library, rather than using a hack.

chestercharles avatar Aug 01 '19 23:08 chestercharles

In my case, this was also a feasible solution:

function fixRNDimensions(orientation: OrientationType) {
  const windowDim = Dimensions.get('window')
  const screenDim = Dimensions.get('screen')
  if (
    (orientation.match(/LANDSCAPE/i) && windowDim.width < windowDim.height) ||
    (orientation.match(/PORTRAIT/i) && windowDim.width > windowDim.height)
  ) {
    console.log('fixing dimensions after rotation', windowDim)
    Dimensions.set({
      screen: {
        ...screenDim,
        width: screenDim.height,
        height: screenDim.width,
      },
      window: {
        ...windowDim,
        width: windowDim.height,
        height: windowDim.width,
      },
    })
  }
}
Orientation.addOrientationListener(fixRNDimensions)

you could also run fixRNDimensions manually when setting the rotation

EyMaddis avatar Jul 24 '20 15:07 EyMaddis

I think I got this solved by using screen dimensions with:

https://github.com/react-native-community/hooks#usedimensions

{height, width} = useDimensions().screen

MorganeAlinai avatar Oct 05 '20 17:10 MorganeAlinai