Majster
Majster

Reputation: 3701

Disable rotation of a UINavigationController in iOS7

I have a UINavigationController which creates an instance of UIViewController and sets another UIViewController as it's root. I then present the navigation controller and everything works.

The problem comes when the user rotates the device. All my controllers (including root and excluding UINavigationController) implement the shouldAutorotate and return FALSE.

Somehow, my views still rotate. We come to the centre of my problem. I create and present the navigation controller like so:

AwesomeViewController *controller = [[AwesomeViewController alloc] init];

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:controller];

[self presentViewController:navController 
                   animated:YES 
                 completion:NULL];

It is pretty straight forward as you can see.

  1. The navController is missing its shouldAutorotate set to FALSE and that's why it's rotating, am I correct?

  2. How to lock it in portrait orientation?

  3. Can it be done without making a ridiculous subclass like this:

    @interface LockedRotationNavigationController : UINavigationController
    @end
    
    @implementation LockedRotationNavigationController
    - (BOOL)shouldAutorotate { return NO; }
    @end
    

What I'm actually looking for is how to disable rotation of the UINavigationController without subclassing it?

The property is readonly so I'm out of ideas on how to do it.

Upvotes: 2

Views: 1927

Answers (1)

Rafał Sroka
Rafał Sroka

Reputation: 40038

To disable rotations globally in your app in Info.plist expand Supported interface orientations and remove Landscape items to make your application only run in portrait mode.

enter image description here

Or you can just select supported orientations in Xcode like this:

enter image description here

You can also lock the rotation programmatically by implementing shouldAutorotateToInterfaceOrientation method in your view controller.

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

Upvotes: 2

Related Questions