10

I've got an app where I have a UINavigationController subclass as my rootViewController. I've got a UITableViewController that lets the user edit some settings, it should always be in portrait mode. My app also needs to support all other orientations after I push a MoviePlayer component onto the navigation controller.

The UITableViewController subclass has this implementation of supportedInterfaceOrientations:

- (NSUInteger)supportedInterfaceOrientations {
    LLog();
    return UIInterfaceOrientationMaskPortrait;
}

The logging command tells me that this gets actually called.

The problem is that the return value is not respected, i.e. the screen turns to landscape orientation when I turn the device.

What can I do to make the settings view always show in portrait but allow orientation changes for the video viewer?

More information: my UINavigationController subclass doesn't override shouldAutorotate or supportedInterfaceOrientations. I haven't implemented

   - (NSUInteger)application:(UIApplication *)application 

supportedInterfaceOrientationsForWindow:(UIWindow *)window

method in my AppDelegate and I have enabled all orientations in the target summary.

swiftBoy
  • 35,607
  • 26
  • 136
  • 135
flovdu
  • 285
  • 2
  • 3
  • 8

3 Answers3

17

I had issue that some ViewControllers in the navigation stack support all the orientations, some only portrait, but UINavigation controller was returning all app supported orientations, this little hack helped me.

@implementation UINavigationController (iOS6OrientationFix)

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

@end
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
Mindaugas
  • 1,707
  • 13
  • 20
  • This is definitely a creative solution but I can't believe this is what Apple expects us to do. I'm having trouble understanding their new autorotation scheme. They say the top full screen ViewController rules. But, like you are pointing out, the top full screen ViewController is in many cases the NavigationController. Are we then supposed to subclass it, or create a category to implement the supportedInterfaceOrientations message? Doesn't feel right. – eddy Sep 25 '12 at 18:08
  • It doesn't feel right to me too, maybe it's just a bug in Apple implementation. – Mindaugas Sep 28 '12 at 16:37
  • This does not work for me. I've tried subclassing as well since Categories don't ensure method override :( – Pacu Sep 30 '12 at 23:06
  • 2
    Thanks for posting, have spent a day trying to get this to work and it is a great workaround @Mindaugas. Think Apple have dropped the ball on this. – Magic Bullet Dave Oct 05 '12 at 16:19
  • See [this answer](http://stackoverflow.com/a/12588038/35690) for a more robust solution, along with a warning as to what can happen if you do end up using these kind of categories. – Senseful Jul 25 '13 at 06:33
2

You also need to add:

- (BOOL)shouldAutorotate {
    return NO;
}

and set the supported rotations for the root view controller in the app plist file to only portrait.

Sverrisson
  • 17,970
  • 5
  • 66
  • 62
2

Category for UINavigationController not working for me. I don't know why. I solve my problem with such category of UIViewController:

@implementation UIViewController (Orientation)

- (BOOL) shouldAutorotate
{
    return YES;
}


- (NSUInteger)supportedInterfaceOrientations
{
    NSUInteger orientations = UIInterfaceOrientationMaskPortrait;

    if ([self isKindOfClass:[PlayerViewController class]])
    {

        orientations |= UIInterfaceOrientationMaskLandscapeLeft;
        orientations |= UIInterfaceOrientationMaskLandscapeRight;

    }

    return orientations;
}

@end
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
Gleb Tarasov
  • 885
  • 11
  • 15