I want an app to maintain the same UI despite of what the device orientation is. I don’t want the default behavior when supporting multiple device orientations where the UI slides / shifts automatically each time the orientation changes. However, I also want to detect what the orientation is in order to perform some custom code.
- UIDevice.orientationDidChangeNotification doesn’t seem to get sent when supportedDeviceOrientations in Info.plist is set to only portrait.
- Overriding supportedInterfaceOrientations didn’t seem to do anything.
Is there a way to get information on the device’s orientation (it WOULD have been in landscape right now) while only allowing for one device orientation in Info.plist?
2
Answers
What you need to do is update your Info.plist to support all orientations.
Then in your root view controller you override
supportedInterfaceOrientations
:This keeps your view controller in portrait regardless of the device orientation but it also allows device orientation change notifications to still be sent as the device orientation changes.
This assumes that rotation lock is not enabled on the iPhone. If the user locks the rotation then an app can’t get device orientation change notifications at all. But that’s basically the point of the rotation lock.
Assuming you take in place @hangarrash answer, to detect iPhone orientation you can use NotificationCenter and some combine publisher.