防止一个视图控制器ios7 autorotate?

我的应用程序可以自动旋转,但我需要其中的一个意见,只能在肖像模式下显示,不知道如何实现这一点。 我尝试了这个(除其他外),但是有问题的视图仍然在旋转:

-(BOOL)shouldAutorotate { return NO; } - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } 

该解决scheme解释了如何控制各个视图控制器的方向,只要它们由导航控制器pipe理。

在Xcode 5中,创build一个types为“Objective-C category”的新文件,将其设置为“Category”为“rotation”,并select“UINavigationController”作为“Category on”。

一个新的文件对将出现在项目中,具有以下名称:UINavigationController + rotation.h UINavigationController + rotation.m

在.m文件中,编写下面的代码:

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

这样,导航控制器将让当前的顶视图控制器确定方向策略。

然后,在由导航控制器pipe理的每个特定视图控制器中,您可以覆盖两个方向相关的方法。

例如,如果一个特定的视图控制器只能以纵向显示:

 - (BOOL) shouldAutorotate { return NO; } - (NSUInteger) supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } 

确保所需的方向是项目部署信息中设置的方向之一。 希望这是足够详细的,可以帮助。

如果您将视图控制器作为模式视图控制器呈现, supportedInterfaceOrientations将可以正常工作。 如果您将其作为导航控制器堆栈的一部分呈现,则不起作用。 如果你希望你的视图以模态方式呈现,但是在导航控制器(例如导航项目)中,我所做的解决scheme是UINavigationController并覆盖我的子类上的supportedInterfaceOrientations方法。