Question

I've got my View hierarchy set up like this.

UITabBarController
    |
    UINavigationController
    |  |
    |  UIViewController
    |
    UINavigationController
       |
       UIViewController

The thing is that I have a ViewController that is only shown in Portrait, as soon as I turn the device to landscape another ViewController is being pushed on top, that is working as intended so far.

What I want now is that as soon as I push the back button on the newly popped ViewController that the old ViewController is being forced to Portrait, even though the Device is still in Landscape.
I have tried it with transitions, but then the other views are getting screwed and don't properly recognize there orientation anymore leading to a mess of displaced views.

Was it helpful?

Solution

The only thing that worked for me was using

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

in my ViewController and presenting it with presentModalViewController. This forces the View to stay in landscape. Not exactly what I wanted, but it worked out.

OTHER TIPS

Try adding this to your original view controller:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (UIInterfaceOrientationIsPortrait(interfaceOrientation)) return YES;
    return NO;
}

This should force it to only display in portrait orientation.

The code below will force the view into landscape mode, but only if the parent view controller supports landscape mode.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

If the parent view controller of the view you want to force into landscape mode is portrait only, you'll have to create a new new navigation controller. The easiest way to do that is just presenting the forced-landscape view controller in a modal view. So use presentModalViewController instead of pushViewController.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top