Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dimensions not correctly set after locking to portrait #71

Open
austinthetaco opened this issue Jun 6, 2019 · 8 comments
Open

Dimensions not correctly set after locking to portrait #71

austinthetaco opened this issue Jun 6, 2019 · 8 comments

Comments

@austinthetaco
Copy link

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?

@michelegoh
Copy link
Contributor

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

@wonday
Copy link
Owner

wonday commented Jun 12, 2019

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;

@austinthetaco
Copy link
Author

@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.

@dellert
Copy link

dellert commented Jul 7, 2019

same issue

@grnkvch
Copy link

grnkvch commented Jul 24, 2019

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);
  };

@chestercharles
Copy link

chestercharles commented Aug 1, 2019

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.

@EyMaddis
Copy link

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

@MorganeAlinai
Copy link

MorganeAlinai commented Oct 5, 2020

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

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

{height, width} = useDimensions().screen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants