wonday / react-native-orientation-locker

A react-native module that can listen on orientation changing of device, get current orientation, lock to preferred orientation.
MIT License
757 stars 274 forks source link

Navigating to new screen and lockToLandscape() immediatelly doesn't rotate on iOS #210

Open rogerkerse opened 2 years ago

rogerkerse commented 2 years ago

When using react-navigation 6, navigating to a screen and having this code doesn't rotate screen.

useEffect(() => {
    Orientation.lockToLandscape()
}, [])

It only works with timeout over 500 (in my case).

CDBridger commented 2 years ago

it doesn't work for me at all

kevingraillot39 commented 2 years ago

Hi, Not working for me too. The screen is rotate but application still in portrait. Any idea ?

achang0406 commented 2 years ago

lockToLandscape and lockToPortrait don't work for me on ios 15. This post suggest force rotation was never intended? https://developer.apple.com/forums/thread/692576

Do people have work arounds?

achang0406 commented 2 years ago

adding shouldAutoRotate fixed my problem

bfbiggs commented 2 years ago

Same issue - is the shouldAutoRotate setup in the Delegate file? @achang0406

Ananthakr commented 2 years ago

@bfbiggs Yes, like this in appdelegate.m

- (BOOL)shouldAutorotate
{
    return FALSE;    
}

Thanks @achang0406

Amol-B-Patil commented 2 years ago

Not any of the solutions listed above worked for me on iOS 13, 14 and 15.

For now I solved the problem by adding setTimeout to lock the screen orientation.

setTimeout(() => {
      Orientation.lockToLandscape();
    }, Platform.select({
  android: 0,
  ios: 600,
}));

On Android, the app automatically locks into the landscape mode so I put the timeout as 0. On iOS, tweak the timeout between 600 to 1000 as per your need, it will automatically rotate the app in landscape mode (It will work for switching from landscape to portrait too).

This will work as expected.

Thank you!

maschad commented 2 years ago

Not any of the solutions listed above worked for me on iOS 13, 14 and 15.

For now I solved the problem by adding setTimeout to lock the screen orientation.

setTimeout(() => {
      Orientation.lockToLandscape();
    }, Platform.select({
  android: 0,
  ios: 600,
}));

On Android, the app automatically locks into the landscape mode so I put the timeout as 0. On iOS, tweak the timeout between 600 to 1000 as per your need, it will automatically rotate the app in landscape mode (It will work for switching from landscape to portrait too).

This will work as expected.

Thank you!

I believe the forced rotation isn't working because it's being reset here https://github.com/wonday/react-native-orientation-locker/blob/733ad2c931867336f00627caf57e73c73da4594b/iOS/RCTOrientation/Orientation.m#L133-L134 I opened a PR to resolve this, I have been using this forked version in production for a few months now.

Champkinz commented 1 year ago

The work around i found was this useEffect(() => { setTimeout(() => { Orientation.lockToLandscape(); }, 1000); }, []);

mujehoxe commented 1 year ago

Experiencing a similar issue, on android when i navigate to the new screen and lock the orientation to landscape it the component doesn't render and then takes me to the main screen with the orientation locked to landscape. When i remove Orientation.lockToLandscape() in the new screen the component renders perfectly, i also tried to set a time out, i am not locking the orientation anywhere else in the application. i also tried unlockingAllOrientations. Not getting any error so there is no way to try and catching.

eliasg52 commented 9 months ago

adding shouldAutoRotate fixed my problem

Does this solution work? Why does the iOS documentation indicate that this property is deprecated? If it works, how do I implement it? thank you