Problem pushViewController from Landscape to Portrait
Asked Answered
I

3

5

I am trying to go from a viewcontroller that supports landscape (while in landscape mode), to one that explicitly doesn't (and shouldn't) support landscape. I'm doing this as follows:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}

Ideally I want the new viewController that i'm pushing onto the stack to start off initially in portrait, not landscape. Strangely even with this method implemented, it starts off in Landscape.

My only guess is that Apple doesn't want a user transitioning from landscape to portrait (despite allowing us to go from landscape, back to a previous controller that is in portrait).

Any insights and/or help would be much appreciated.

Infliction answered 27/1, 2011 at 21:56 Comment(0)
I
10

I found a way to force portrait. It's a bit a hack, but here it is. In the -(void)viewDidLoad of the viewController that you want to force portrait for do the following:

UIViewController *viewController = [[UIViewController alloc] init];
[self presentModalViewController:viewController animated:NO];
[self dismissModalViewControllerAnimated:NO];
[viewController release];

This basically forces portrait, by presenting a controller (which only supports portrait by default).

Infliction answered 18/2, 2011 at 22:8 Comment(2)
This did it for me moving from portrait-only to landscape-only view via pushViewController. Saves you having to call undocumented private API [[UIDevice currentDevice] setOrientation:UIDeviceOrientationLandscapeRight] Thanks!Sheasheaf
nice hack... came real handy... :)Dysuria
P
4

You will need to present your new view controller modally. If your view controller exists within a navigation controller the orientation of all view controllers in the nav stack is implied by the root view controller in the stack. Whatever your root view controller in the nav stack returns from shouldAutoRotateToInterfaceOrientation is then used for all view controllers below it.

Pace answered 28/1, 2011 at 17:32 Comment(1)
Ah I see, this is what I assumed was the case. Thanks for the clarification!Infliction
E
1

The answer by Sahil above is deprecated since iOS 6.0. However, the following seems to do the same trick:

UIViewController *viewController = [[UIViewController alloc] init];
[self presentViewController:viewController animated:NO completion:nil];
[self dismissViewControllerAnimated:NO completion:nil];
[viewController release];
Elver answered 6/3, 2013 at 10:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.