【问题标题】:How to disable autorotation in iOS 8 for a particular controller?如何在 iOS 8 中为特定控制器禁用自动旋转?
【发布时间】:2014-10-30 15:06:34
【问题描述】:
-(BOOL)shouldAutorotate {

    return NO;

}

上述方法适用于一个控制器,但当有多个视图控制器被压入堆栈时。
我想要一个仅应以纵向模式显示的特定控制器。

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

}

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

【问题讨论】:

    标签: ios objective-c ios8 uiinterfaceorientation


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 这是正确的做法。但在我的情况下,我还必须覆盖 NavigationController 的 [UIViewController shouldAutorotate](对其进行子类化)以返回 navigationController.topViewController.shouldAutorotate。
    • 另外,别忘了检查 topViewController 是否为空
    猜你喜欢
    • 2015-08-30
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    • 2016-07-05
    • 1970-01-01
    • 1970-01-01
    • 2014-10-02
    • 1970-01-01
    相关资源
    最近更新 更多