Graham
Graham

Reputation: 6562

Modal UINavigationController presented in wrong orientation

I have a setup where an iPad app supports all orientations. In part of the application, a tap triggers the display of a modal view controller which requires a navigation controller. So I'm basically doing:

UIViewController *myViewController = [[UIViewController alloc] initWithNibName:@"MyView" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:myViewController];
NSLog(@"%d", nav.interfaceOrientation);

[window.rootViewController presentModalViewController:nav animated:YES];

This works fine when the iPad is in portrait (home button). In any other orientation, the modal view always appears in portrait from the home button rather than the current bottom of the device.

If I skip the navigation controller completely and just show myViewController modally, it appears correctly.

So, I tried subclassing the UINavigationViewController and explicitly overriding the shouldAutorotate: and supportedInterfaceOrientations: methods, but this doesn't have any effect. Interestingly if I override the viewWillAppear method and log the value of the interfaceOrientation property, this always returns 1 even if it returns the correct value when accessing it from the code above.

I've also checked that all orientations are supported in Info.plist.

So, what else could be happening to cause this behaviour? Any help greatly appreciated.

Upvotes: 1

Views: 213

Answers (1)

Andre M F
Andre M F

Reputation: 53

Try to implement preferredInterfaceOrientationForPresentation in your subclass of the navigation controller.

The following worked for me:

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return self.interfaceOrientation;
}

Upvotes: 1

Related Questions