iOS 6 SupportedOrientations with UINavigationController

You probably already know that -shouldAutorotateToInterfaceOrientation had been deprecated in iOS 6 and we were told to move to -shouldAutorotate and -supportedInterfaceOrientations, but me and apparently many others on SO and on the Apple forums were having trouble if the rootViewController in the AppDelegate was anything besides a UIViewController.

So here’s my solution that could probably work if you subclassed UITabBarController also.

My subclassed UINavigationController that is Orientation Aware of it’s top-most ViewController.

(I will provide the class files at the bottom of the post)

So First of all you’ll override these 2 methods so that the top viewcontroller of the hierarchy will take control of the orientation.

-(NSUInteger)supportedInterfaceOrientations
{
    return [self.topViewController supportedInterfaceOrientations];
}

-(BOOL)shouldAutorotate
{
    return YES;
}
Now whichever ViewController’s you don’t want to rotate add these lines of code.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}
And for any ViewController that you do want to be able to rotate and code like this
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    }

    -(NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }

    -(BOOL)shouldAutorotate
    {
        return YES;
    }

Here are the class files for the Orientation-aware UINavigationcontroller

This was posted 2 years ago. It has 9 notes and 0 comments.

  1. nicedexter reblogged this from shabzcode
  2. webcoderph reblogged this from shabzcode
  3. cocoamex reblogged this from shabzcode
  4. shabzcode posted this