-
Notifications
You must be signed in to change notification settings - Fork 274
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
Comments
This is a react native problem I think, I've been using View's onLayout method to get the screen width and height |
Dimensions.get('window') seems not changed after app started. when Landscape |
@wonday It is changed after app start. Upon app launch with |
same issue |
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);
}; |
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
It would definitely be nice to have this bug fixed in the library, rather than using a hack. |
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 |
I think I got this solved by using screen dimensions with: https://github.com/react-native-community/hooks#usedimensions
|
Currently I am calling
Orientation.lockToPortrait
on app load, then for a single component i'm callinglockToLandscape
and upon leaving that component again callinglockToPortrait
. However when i console log outDimensions.get('window');
after the lastlockToPortrait
the width is still larger than the height. Any idea if there is a fix for this?The text was updated successfully, but these errors were encountered: