Can you disable rotation globally in an iOS app?

2020-05-25 04:27发布

I have an app made up of a lot of view controllers... in the project summary I've set Portrait orientation as the only supported device orientation.

However, the app still gets messed up when turned sideways.

My question is, is there a way to globally disable autorotation through app delegate or something?

Or do I have to go into all of my view controllers and add the "shouldAutorotateToInterfaceOrientation" method?

Just don't want to miss adding it to one or something...

Thanks!

标签: ios xcode
6条回答
趁早两清
2楼-- · 2020-05-25 04:34

There are three kinds of Device orientation keys there in the info.plist now.

  1. Supported interface orientations (iPad)
  2. Supported interface orientations (iPhone)
  3. Supported interface orientations

Third one is I think for non-universal apps and rest two above are for iPad and iPhone.

You should give them a try.

enter image description here

查看更多
兄弟一词,经得起流年.
3楼-- · 2020-05-25 04:38

in root view controller's method:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

set 'return NO';

this should do for all the Views.

查看更多
家丑人穷心不美
4楼-- · 2020-05-25 04:42

Haris Hussain's answer appears to be deprecated now, as of IOS 6, but there are new methods available for limiting/enabling rotation.

Here are the methods listed in the UIViewController header:

// New Autorotation support.
- (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0);
- (UIInterfaceOrientationMask)supportedInterfaceOrientations NS_AVAILABLE_IOS(6_0);
// Returns interface orientation masks.
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation NS_AVAILABLE_IOS(6_0);

Note that shouldAutoRotate doesn't seem to work if you start the app in an already rotated state!

查看更多
女痞
5楼-- · 2020-05-25 04:45

In Info.plist expand "Supported interface orientations" and remove Landscape items to make your application only run in portrait mode.

查看更多
Ridiculous、
6楼-- · 2020-05-25 04:55

If you're supporting iPad, then you SHOULD NOT uncheck the landscape orientations, as it will prevent your app from being accepted by Apple on App Store.

To prevent rotating before the app shows your first screen, put this inside your AppDelegate.m

This method works and tested in iOS 7.1 above.

// G - fix for ipad.
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return UIInterfaceOrientationMaskPortrait;
}
查看更多
对你真心纯属浪费
7楼-- · 2020-05-25 04:57

After struggling to set in UIViewController's shouldAutorotate and supportedInterfaceOrientation methods, with no success in iOS6, I found the most effective is to set it in app delegate.

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return UIInterfaceOrientationMaskPortrait;
}

However returning UIInterfaceOrientationMaskPortraitUpsideDown was crashing my app. I dont know whats wrong I was doing!

查看更多
登录 后发表回答