Force Landscape iOS 6

2019-05-26 05:19发布

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!

2条回答
Juvenile、少年°
2楼-- · 2019-05-26 06:07

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.

查看更多
做自己的国王
3楼-- · 2019-05-26 06:13

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.

查看更多
登录 后发表回答