0

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?

Rolf Staflin
  • 2,092
  • 2
  • 22
  • 19

2 Answers2

1

You cannot by any means prevent an iPad app that can assume all four rotational position from actually assuming all four rotational positions, unless you explicitly opt out of iPad multitasking.

To do so, set the Info.plist key UIRequiresFullScreen to YES; you can do that conveniently while editing the app target by checking Requires Full Screen in the General tab.

But Apple warns that this option is slated to be removed; multitasking will become a requirement and thus rotation to any position will be required as well. Basically it would be better to change your desires.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thank you matt, I had already checked "Requires full screen" in the app target's general tab. It had no effect, but I found that there are _two_ flags for this in the Info.plist file: `UIRequiresFullScreen` and `UIRequiresFullScreen-ipad`. Once I set the iPad flag to yes in the `-Info.plist`, it worked perfectly! – Rolf Staflin Mar 13 '21 at 19:26
  • It’s silly to have both flags, as this setting is meaningful only on iPad anyway. Just deleting the second one would be enough. The introduction of the second flag was a bug all along. Delete it and let the first one do its job. – matt Mar 13 '21 at 20:33
  • But, as I say, you would probably do better to accept the inevitable and let the device rotate. – matt Mar 13 '21 at 20:38
  • Yes, if I designed the app today I would do so – but this is an ancient app from 2009 that I'm keeping alive. If/when multitasking becomes mandatory I realize I'll have to do more rework, but for now I am happy to have a working app! – Rolf Staflin Mar 14 '21 at 09:35
0

I had asked a simmilar Question for an Ipad but i got closed because of this Question. My Solution for Ipad IOS 14.5 and VisualStudio2019 was adding these Lines in the InfoPlist. (For beginners Like me: don't open the Manifest Mask, rightclick the "infoPlist" unter the IOS Header and select "view Code".) Then add these Missing lines:

<key>UISupportedInterfaceOrientations~ipad</key>
<array>
<string>UIInterfaceOrientationPortrait</string>    <!--For Vertikal-->
<string>UIInterfaceOrientationLandscapeLeft</string> <!--For Horizontal-->
<string>UIInterfaceOrientationLandscapeRight</string> <!--For Horizontal-->
</array>
Dharman
  • 30,962
  • 25
  • 85
  • 135