简体   繁体   中英

Locking screen orientation in iOS 14

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?

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM