I'm trying to implent the Ipad Landscape mode but the device orientation is not being detected. We have made all the necessary changes in info.plist file.Still we are not able to get the windowWidth and windowHeight value according to the device orientation. All the time the windowWidth and windowHeight values remains the same.
Below is the code we tried...
const windowWidth = Dimensions.get("window").width;
const windowHeight = Dimensions.get("window").height;
const isIpad = windowWidth >= 768;
const [orientation, setOrientation] = useState(getInitialOrientation());
useEffect(() => {
const updateOrientation = () => {
const newOrientation = getOrientation();
if (newOrientation !== orientation) {
setOrientation(newOrientation);
}
};
Dimensions.addEventListener('change', updateOrientation);
return () => {
Dimensions.removeEventListener('change', updateOrientation);
};
}, [orientation]);
const getInitialOrientation = () => {
return windowWidth < windowHeight ? 'PORTRAIT' : windowWidth > 768 ? 'LANDSCAPE' : 'PORTRAIT'; // Adjust the width condition as per your requirement
}
const getOrientation = () => {
return windowWidth < windowHeight ? 'PORTRAIT' : windowWidth > 768 ? 'LANDSCAPE' : 'PORTRAIT'; // Adjust the width condition as per your requirement
}
We expected the windowWidth and windowHeight values to change when we turned the device orientation but it is not happening.
This code would execute only once initially:
Use the parameter passed to the callback
updateOrientation->({ window: { width, height } })I have created an example: