Prevent autorotate for one view controller ios7?

北战南征 提交于 2019-12-31 11:10:26

问题


My app can autorotate but I need one of the views to only show in portrait mode and don't know how to achieve this. I tried this (among other things) but the view in question still rotates:

-(BOOL)shouldAutorotate
{            
    return NO;
}

 - (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

回答1:


This solution explains how to control orientation on individual view controllers, provided they are managed by a navigation controller.

In Xcode 5, create a new file of type "Objective-C category", set it's "Category" to "rotation" and choose "UINavigationController" as "Category on".

A new file couple will appear in the project, having the following names: UINavigationController+rotation.h UINavigationController+rotation.m

In the .m file, write the following code:

- (BOOL) shouldAutorotate
{
    return [[self topViewController] shouldAutorotate];
}

- (NSUInteger) supportedInterfaceOrientations
{
    return [[self topViewController] supportedInterfaceOrientations];
}

This way, the navigation controller will let the current top view controller determine the orientation policy.

Then, in each specific view controller that is managed by the navigation controller, you can override the two orientation-related methods.

For instance, if a specific view controller shall appear in portrait orientation only:

- (BOOL) shouldAutorotate
{            
    return NO;
}

 - (NSUInteger) supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

Make sure that the desired orientation is one of those set in the project deployment info. Hope this is sufficiently detailed and can be of help.




回答2:


supportedInterfaceOrientations will work if you present your view controller as a modal view controller. It won't work if you present it as part of a navigation controller stack. If you want your view presented modally but inside a navigation controller (to have navigation items, for instance) the solution I did was to subclass UINavigationController and override the supportedInterfaceOrientations methods on my subclass.



来源:https://stackoverflow.com/questions/19601235/prevent-autorotate-for-one-view-controller-ios7

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!