IOS 6 Tab Bar app: shouldAutorotate not working

I am rapidly developing an app with a tab bar and some navigation managers in Storyboard using iOS 6 and Xcode 4.5

Usually an application should support all interface orientations, but I have two views that should only support portrait mode.

So, I added the following code to the view controllers:

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationPortrait;
}

      

In another app I developed without storyboard and navigation controller, on iOS 6 it works, but its NOT!: /

I hope someone can help, because I found another post where not helpful ...

Best wishes from Germany

Lorenz

EDIT:

I tried it too - doesn't work!

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;

} 

      

+3


source to share


2 answers


Great answer by Jonathan.

I modified my code a bit to handle the navigation controller in one chunk.



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

      

0


source


As far as I can tell, this issue occurs because UITabBarController and UINavigationController return their own defaults for - (BOOL) shouldAutorotate and - (NSUInteger) supportedInterfaceOrientations.

One solution is to extend both of these classes through categories (or just a subclass) 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 add this to 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 of your view controllers will continue to autorotate. In two view controllers that should only support portrait mode, do the following:

-(BOOL)shouldAutorotate {
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

      

+6


source







All Articles