防止一个视图控制器autorotate?

我的应用程序可以自动旋转,但我需要其中的一个意见,只能在肖像模式下显示,不知道如何实现这一点。

我尝试了这个(除其他外),但是有问题的视图仍然在旋转:

// ViewController.m -(BOOL)shouldAutorotate { return NO; } - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; } 

有人能指出我做错了什么吗? 谢谢。

-编辑-

这是iOS 6.1

当涉及UINavigationController时,在UINavigationController上创build一个类别并override supportedInterfaceOrientations

  #import "UINavigationController+Orientation.h" @implementation UINavigationController (Orientation) -(NSUInteger)supportedInterfaceOrientations { return [self.topViewController supportedInterfaceOrientations]; } -(BOOL)shouldAutorotate { return YES; } @end 

现在,iOS容器(如UINavigationController)不会咨询他们的孩子,以确定他们是否应该自动旋转。

如何创build一个类别
1.添加一个新文件(cocoa触摸下的Objective c- category)
2. Category :UINavigationController的方向
3.将上面的代码添加到UINavigationController+Orientation.m

Swift 3版本接受的答案是:

 extension UINavigationController { open override var supportedInterfaceOrientations: UIInterfaceOrientationMask { // Change `.portrait` to whatever your default is throughout your app return topViewController?.supportedInterfaceOrientations ?? .portrait } open override var shouldAutorotate: Bool { return true } }