2

I am looking to have one view in my app have landscape orientation. I have managed to get the view to stay in landscape when rotated manually, but if the device is already portrait, it stays portrait, regardless of its supported orientation (set using supportedInterfaceOrientations method) . Is there a way to get the view to rotate automatically? I have tried:

[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];

but this doesn't work.

Any suggestions would be greatly appreciated!

ABC
  • 718
  • 8
  • 23
  • possible duplicate of [IOS 6 force device orientation to landscape](http://stackoverflow.com/questions/12640870/ios-6-force-device-orientation-to-landscape) – David Rönnqvist Apr 22 '13 at 18:26

2 Answers2

2

One way to do this is by overriding preferredInterfaceOrientationForPresentation but in order for that to be called the viewController has to be presented (as in modal) and not pushed as mentioned here:

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    NSLog(@" preferred called");
    return UIInterfaceOrientationPortrait;
}

In order to present your viewController in a UINavigationController use:

UINavigationController *presentedNavController = [[UINavigationController alloc] initWithRootViewController:protraitViewController];

[self presentViewController:presentedNavController animated:YES completion:nil];

To make UINavigationController respect your current viewController's orientation preferences use this simple category instead of sub-classing.

Also, this part of Apple's documentation is a good read for understanding iOS orientation handling better.

Community
  • 1
  • 1
Mariam K.
  • 600
  • 4
  • 13
  • I found this question to be similar with a slightly different answer that apparently worked for a lot of people http://stackoverflow.com/questions/12640870/ios-6-force-device-orientation-to-landscape?rq=1 – Mariam K. Apr 22 '13 at 09:11
-1

Define the following in the UIViewController for your landscape-only view:

- (UIInterfaceOrientation) supportedInterfaceOrientations
{ return UIInterfaceOrientationLandscapeLeft; } // or right or both

This should prevent your view from ever appearing in portrait. From iOS documentation:

Declaring a Preferred Presentation Orientation When a view controller is presented full-screen to show its content, sometimes the content appears best when viewed in a particular orientation in mind. If the content can only be displayed in that orientation, then you simply return that as the only orientation from your supportedInterfaceOrientations method.

GoZoner
  • 67,920
  • 20
  • 95
  • 145
  • I have done this already using: `return UIInterfaceOrientationLandscapeRight| UIInterfaceOrientationLandscapeLeft;` in my code. Sorry I forgot to include it in my question – ABC Apr 21 '13 at 15:56
  • This will never call. Please check. – ani Mar 05 '14 at 10:39