I am updating an old iPad app, but I'm unable to stop iOS from rotating a controller that should only be viewed in portrait mode. The app has a UISplitViewController
, but at one point I need to present another controller fullscreen in portrait mode regardless of whether the iPad was in portrait or landscape before.
I have two issues:
- If the UI was in landscape mode, the portrait view controller also appears in landscape
- The rotation is no longer blocked, so the user can rotate the device even if the controller was displayed in the correct orientation
The documentation says that all rotation-related methods were deprecated in iOS 8, and instead iOS will call viewWillTransitionToSize on the window's root viewcontroller. I am therefore calling [window setRootViewController]
to setup my portrait-only controller, and indeed iOS calls viewWillTransitionToSize
on my controller. However, at that point it's already too late! I need to stop the transition before it begins.
After spending many hours googling and trying variations, I am no closer to a solution – there is so much old stuff on the 'net (and here on Stack Overflow) that it's really hard to find current information.
I have tried setting modalPresentationStyle = UIModalPresentationFullScreen
both in viewDidLoad
, viewWillAppear
and viewDidAppear
, and then overriding preferredInterfaceOrientationForPresentation
. My override is never called.
I still have the old methods supportedInterfaceOrientations
, shouldAutorotate
and shouldAutorotateToInterfaceOrientation
, but none of them is ever called.
I tried implementing application:supportedInterfaceOrientationsForWindow
in my app delegate, but the method is never called. (from the answers to this question)
What's the correct way of doing this on iOS 14? Should I use a modal full-screen presentation? Or use the trait environment with UITraitCollection
somehow?