2014-10-30 88 views
1
-(BOOL)shouldAutorotate { 

    return NO; 

} 

上面的方法适用于一个控制器,但是当有多个viewController在堆栈上推送时。
我想要一个特定的控制器,只能以纵向模式显示。如何在iOS 8中禁用特定控制器的自动旋转?

- (void) viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator { 

} 

我已经使用上面的方法建议在iOS 8的堆栈溢出,但它没有给出预期的结果。

回答

8

首先,使用-supportedInterfaceOrientations而不是-shouldAutorotate-shouldAutorotate只能在必须根据运行时确定的因素禁止自动旋转时使用。你知道你的视图控制器总是只支持纵向模式,这里没有运行时决定。

接下来,导航控制器的代理必须实现-navigationControllerSupportedInterfaceOrientations:方法,以在导航堆栈顶部的视图控制器上返回调用-supportedInterfaceOrientations的结果。

-(NSUInteger)navigationControllerSupportedInterfaceOrientations:(UINavigationController *)navigationController { 
    return navigationController.topViewController.supportedInterfaceOrientations; 
} 

一个重要的警告:推到导航堆栈上的视图控制器无法控制其初始界面方向;这将始终是当前的界面方向。上述技术将做的是防止界面在显示视图控制器时旋转到除肖像以外的任何方向。

+0

男人,我解决你..你解决了我的问题.... – 2015-09-08 12:23:26

+0

这是做到这一点的正确方法。 但在我的情况下,我也必须重写NavigationController的[UIViewController shouldAutorotate](继承它)以返回navigationController.topViewController.shouldAutorotate。 – befstrat 2015-09-15 08:55:00

+0

另外,不要忘记检查topViewController是否为空 – henon 2015-10-05 15:13:00