UIDevice.current.setValue(value, forKey: "orientation") does not work if phone held in landscape and tilted 45 - 90 degrees

Viewed 4943

I'm using

let value = UIInterfaceOrientation.landscapeRight.rawValue
UIDevice.current.setValue(value, forKey: "orientation")

to force the presenting view controller to show in landscape right. This is part of a navigation controller flow. This works as expected, but if the phone is held in landscape and titled between 45 and 90 degrees the view will present in portrait. The method is called and the expected value is correct, but no change to landscape occurs. This can be reproduced in a bare bones project with a navigation controller.

Does anyone have any idea what might cause this behavior?

3 Answers

I also had this weird issue that when you rotate your device to the orientation you want to change when entering the next view controller then the screen stays in portrait mode. I managed to solve this issue by calling

UINavigationController.attemptRotationToDeviceOrientation()

after

UIDevice.current.setValue(value, forKey: "orientation")

was called. I guess this will only work if you're using UINavigationController.

Related