Question

I have a question about iOS 6 Orientation.Here is my file https://www.dropbox.com/s/f8q9tghdutge2nu/Orientations_iOS6.zip

In this sample code,I want to make the MasterViewController only has a Portrait Orientation and the DetailViewController has a Portrait Orientation,Landscape Orientation.

I know iOS 6 Orientation is controlled by top-most controller.

So I custom a UINavigationController(CustomNavigationController), set supportedInterfaceOrientations and shouldAutorotate in that class.

-(NSUInteger)supportedInterfaceOrientations{
    if([[self topViewController] isKindOfClass:[DetailViewController class]]){
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }else{
        return UIInterfaceOrientationMaskPortrait;
    }
}

-(BOOL)shouldAutorotate
{
    return YES;
}

Everything is fine except when DetailViewController at Landscape Orientation press back button,MasterViewController will show the Landscape Orientation.

Can I let MasterViewController always show Portrait Orientation and DetailViewController can has many orientation?

thanks!

Was it helpful?

Solution

I made this work as you suggested in your comment on the question. The problem is that the default UINavigatonController does not use the value of the top view controller, so you need to override it by creating a base class and setting it in the Storyboard as the base class.

Below is the code that I use.

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

I also have a base class for the rest of my View Controllers to default the behavior to use the Portrait orientation. I can override these methods for iOS 5 and 6 in any view controllers which support more than Portrait orientation.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}

- (BOOL)shouldAutorotate {
    return FALSE;
}

OTHER TIPS

Thanks! Brennan,
I also collect others way to do that in my blog.
http://blog.hanpo.tw/2012/09/ios-60-orientation.html

Here's the two others way.

1.add a Category to UINavigationController

    @implementation UINavigationController (Rotation_IOS6)

    -(BOOL)shouldAutorotate
    {
        return [[self.viewControllers lastObject] shouldAutorotate];
    }

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

    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
    {
        return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
    }

    @end

2.Swap Method Implementations (Made by spoletto)

https://gist.github.com/3725118

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top