如何在iOS 8中为特定控制器禁用自动旋转? [英] How to disable autorotation in iOS 8 for a particular controller?

查看:106
本文介绍了如何在iOS 8中为特定控制器禁用自动旋转?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-(BOOL)shouldAutorotate {

    return NO;

}

以上方法适用于一个控制器但有多个viewControllers时推到一个堆栈。


我想要一个特定的控制器,只能以纵向模式显示。

Above method works for one controller but when there are multiple viewControllers pushed on a stack.
I want a particular controller that should be displayed in portrait mode only.

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

}

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

I have used above method suggested on stack overflow for iOS 8 but it does not give desired result.

推荐答案

首先,使用 -supportedInterfaceOrientations 而不是 -shouldAutorotate 。只有在必须根据运行时确定的因素禁止自动旋转时,才应使用 -shouldAutorotate 。您知道您的视图控制器将始终仅支持纵向模式,此处没有运行时决定。

First, use -supportedInterfaceOrientations instead of -shouldAutorotate. -shouldAutorotate should only be used when you must disallow autorotation based on factors determined at runtime. You know your view controller will always only support portrait mode, there is no runtime decision here.

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

Next, your navigation controller's delegate must implement the -navigationControllerSupportedInterfaceOrientations: method to return the result of calling -supportedInterfaceOrientations on the view controller at the top of the navigation stack.

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

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

An important caveat: A view controller pushed onto a navigation stack has no control over its initial interface orientation; that will always be the current interface orientation. What the above technique will do is prevent the interface from rotating to any orientation other than portrait while that view controller is displayed.

这篇关于如何在iOS 8中为特定控制器禁用自动旋转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆