3

I momently developing a application with a tab bar and some navigation view controllers in Storyboard using iOS 6 and Xcode 4.5

Usually the app should support all interface orientations but I have two views that only should support portrait mode.

So I added the following code to the view controllers:

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationPortrait;
}

On a other app I developed without storyboard and navigation view controller on iOS 6 it works but her NOT! :/

I hope someone can help, because I found some other post that where not helpful...

With best regards from Germany

Laurenz

EDIT:

I also tried - Doesn't work! :

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;

} 

2 Answers 2

6

As far as I can tell, this problem arises because the UITabBarController and UINavigationController are returning their own default values for -(BOOL)shouldAutorotate and -(NSUInteger)supportedInterfaceOrientations.

One solution is to extend both of those classes via categories (or just subclass), in order to return the appropriate values from your own implementations of these methods in your view controller. This is what worked for me (you can just drop this into your App Delegate):

@implementation UITabBarController(AutorotationFromSelectedView)

- (BOOL)shouldAutorotate {
    if (self.selectedViewController) {
        return [self.selectedViewController shouldAutorotate];
    } else {
        return YES;
    }
}

- (NSUInteger)supportedInterfaceOrientations {
    if (self.selectedViewController) {
        return [self.selectedViewController supportedInterfaceOrientations];
    } else {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
}

@end

@implementation UINavigationController(AutorotationFromVisibleView)

- (BOOL)shouldAutorotate {
    if (self.visibleViewController) {
        return [self.visibleViewController shouldAutorotate];
    } else {
        return YES;
    }
}

- (NSUInteger)supportedInterfaceOrientations {
    if (self.visibleViewController) {
        return [self.visibleViewController supportedInterfaceOrientations];
    } else {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
}
@end

By default, all your view controllers will continue to autorotate. In the two View Controllers that should only support portrait mode, implement the following:

-(BOOL)shouldAutorotate {
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}
3
  • I implemented your code in appdelegate.m and in my viewcontroller.m. It is now locking landscape mode of viewcontroller.m but when I come from previous view to viewcontroller.m in landscape mode then it shows the screen in landscape mode and this time it do not change to potrait mode.
    – mohsin.mr
    Jul 15, 2013 at 10:59
  • This is the correct answer. Ive been having loads of trouble with this and categorising UITab and UINav fixed it. You should mark this answer as correct. Feb 3, 2014 at 14:33
  • I've extended this code to support UISplitViewController too: gist.github.com/cameroncooke/a9244bc4d677f50940f5
    – Camsoft
    Oct 20, 2015 at 8:54
0

Excellent answer by Jonathan.

I modified his code little bit to handle navigation controller in a single snippet.

- (BOOL)shouldAutorotate {
    if (self.selectedViewController) {
        if ([self.selectedViewController isKindOfClass:[UINavigationController class]]) {
            return [[[(UINavigationController*)self.selectedViewController viewControllers] lastObject] shouldAutorotate];
        }
        return [self.selectedViewController shouldAutorotate];
    } else {
        return YES;
    }
}

- (NSUInteger)supportedInterfaceOrientations {
    if (self.selectedViewController) {
        if ([self.selectedViewController isKindOfClass:[UINavigationController class]]) {
            return [[[(UINavigationController*)self.selectedViewController viewControllers] lastObject] supportedInterfaceOrientations];
        }
        return [self.selectedViewController supportedInterfaceOrientations];
    } else {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }
} 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.