【问题标题】:pushViewController: - how to display in portrait only mode, when device is in landscape mode?pushViewController: - 当设备处于横向模式时,如何以仅纵向模式显示?
【发布时间】:2011-06-28 09:59:53
【问题描述】:

我有 2 个视图控制器、根和细节。根视图控制器支持横向和纵向,所以它有如下代码:

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        return (interfaceOrientation == UIInterfaceOrientationPortrait
        || interfaceOrientation == UIInterfaceOrientationLandscapeLeft
        || interfaceOrientation == UIInterfaceOrientationLandscapeRight
        || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
    }

上面的代码非常适合根视图控制器。

如果根视图控制器正在显示,并且用户将设备旋转到横向模式,则视图会相应调整。从这一点开始,如果将我的详细视图控制器推入堆栈,它将以横向模式加载。但是,它不应该,因为我将它配置为仅支持纵向模式。我在详细视图控制器中使用以下代码:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

【问题讨论】:

    标签: iphone sdk uiviewcontroller orientation pushviewcontroller


    【解决方案1】:

    在您的第二个视图控制器中,将您的代码替换为 ...

    return UIInterfaceOrientationIsPortrait( interfaceOrientation );
    

    【讨论】:

    • 这只是一个宏,最终会运行我已经拥有的相同代码。但是,只是为了好玩,我还是尝试了它……我收到了相同的结果。我的第二个视图以横向模式加载,而不是纵向...
    • 是的,但它也会为您处理 PortraitUpsideDown。
    • 非功能性旋转的原因是 shouldAutorotate... 仅在物理设备旋转时调用。你应该检查这个问题 - *.com/questions/2922919/…
    • 感谢您的链接。我想这个故事的寓意是 sdk 不能很好地与基于导航控制器的应用程序配合使用,这些应用程序的视图具有混合的方向。该链接确实进入了一个可能就足够的棘手解决方法。但是,现在我只是想我会全部肖像,或者全部肖像/风景。混合起来很头疼,我看得出来。