2

I have some views in my app that I don't want to suport orientation. In didFinishLaunchingWithOptions I add navigation:

...
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:self.viewController];

    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];
...

In each ViewController I have UITabBar (I don't know if this is important).

In the first view controller I add:

-(BOOL)shouldAutorotate {
        return NO;
    }

    - (NSUInteger)supportedInterfaceOrientations {
        return UIInterfaceOrientationMaskPortrait;
    }

supportedInterfaceOrientations is called at the view loading but shouldAutorotate doesn't call as I rotate device.
What am I missing here?

1110
  • 7,829
  • 55
  • 176
  • 334

2 Answers2

15

It's because neither UITabBarcontroller nor UINavigationController is passing shouldAutorotate to its visible view controller. To fix that you may subclass either UITabBarController or UINavigationController and forward shouldAutorotate from there:

In your subclassed UITabBarController add:

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

In your subclassed UINavigationController add:

- (BOOL)shouldAutorotate
{
    return [self.visibleViewController shouldAutorotate];
}
John Topley
  • 113,588
  • 46
  • 195
  • 237
Tomasz Zabłocki
  • 1,326
  • 7
  • 14
  • Can you explain me what do you mean by "subclass UINavigationController", I know make a class that inherits from UINavigationController but what to do with it, where to load it when I make it? – 1110 Oct 22 '12 at 06:41
  • To subclass it create new class and call it `MyNavigationController` (make sure it's superclass is set to `UINavigationController`). In that newly created class add method `shouldAutorotate` which I mentioned earlier. Do the same for your `UITabBarController`. You should simply use it like: `MyNavigationController *nav = [[MyNavigationController alloc] initWithRootViewController:self.viewController];`. Similary for your new `MyUITabBarController`. If you are using interface builder then simply set Class attribute there to be pointing to your new class. – Tomasz Zabłocki Oct 22 '12 at 08:49
  • Ok, I have made custom navigation controller and add shouldAutoRotate in it. There is no selectedViewController only presentedViewController. I don't need to subclass UITabBarController because I use only UITabBarView in each view. But now rotation doesn't work anywhere. So how to enable it in some view if I did all right. – 1110 Oct 22 '12 at 11:24
  • For `UINavigationController` you should use `visibleViewController` as in my reply. Did you try with debugger, is `shouldAutorotate` called in your custom `UINavigationController`? – Tomasz Zabłocki Oct 22 '12 at 11:36
0

in the AppDelegate:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window  // iOS 6
{

return UIInterfaceOrientationMaskAll;


}

in your ViewController:

- (BOOL)shouldAutorotate {
return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
Mutawe
  • 6,464
  • 3
  • 47
  • 90