sayguh
sayguh

Reputation: 2550

Can you disable rotation globally in an iOS app?

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!

Upvotes: 27

Views: 41608

Answers (7)

slushy
slushy

Reputation: 12385

Swift
iOS 6+

The recommended way is to override shouldAutorotate in every attached view controller. If you override this property in the root view controller, then all "attached" view controllers will also inherit the behavior. Presented view controllers, however, can become "unattached" to the root and thus would not inherit the override and so you would need to override the property in those view controllers separately.

class RootViewController: UIViewController {
    override var shouldAutorotate: Bool { false }
}

class PresentedViewController: UIViewController {
    override var shouldAutorotate: Bool { false }
}

However, if you simply subclass UIViewController and only use the subclassed view controllers in your project then you have a truly global fix (using code).

class NonrotatableViewController: UIViewController {
    override var shouldAutorotate: Bool { false }
}

Settings alternative (no code)

Perhaps the most global fix is editing the Info.plist file in the info tab in the target's settings. There is a supported-interface-orientations key with each value representing a supported orientation. Deleting all but portrait will disable rotation in all view controllers, attached or unattached to the root.

Upvotes: 0

Vaibhav Saran
Vaibhav Saran

Reputation: 12908

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

Upvotes: 25

Glenn Posadas
Glenn Posadas

Reputation: 13281

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;
}

Upvotes: 3

Supahfly
Supahfly

Reputation: 819

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

Upvotes: 75

James Wolfe
James Wolfe

Reputation: 360

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!

Upvotes: 0

karim
karim

Reputation: 15589

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!

Upvotes: 14

Haris Hussain
Haris Hussain

Reputation: 2581

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.

Upvotes: 6

Related Questions