iOS 6 : How do I restrict some views to portrait and allow others to rotate?

标签 ios iphone ios6 autorotate

我有一个 iPhone 应用程序,它使用 UINavigationController 来呈现一个向下钻取的界面:第一个 View ,然后是另一个 View ,最多四个层次。我希望前三个 View 仅限于纵向,只有最后一个 View 可以旋转为横向。当从第四个 View 返回到第三个和第四个 View 时,我希望所有内容都旋转回纵向。

在 iOS 5 中,我只是在我的每个 View Controller 中定义了 shouldAutorotateToInterfaceOrientation: 以针对允许的方向返回 YES。一切都按上述方式工作,包括返回纵向,即使设备在从 View Controller #4 返回到 #3 时处于横向。

在 iOS 6 中,所有 View Controller 都旋转为横向,打破了那些原本不应该的。 iOS 6 发行说明说

More responsibility is moving to the app and the app delegate. Now, iOS containers (such as UINavigationController) do not consult their children to determine whether they should autorotate. [...] The system asks the top-most full-screen view controller (typically the root view controller) for its supported interface orientations whenever the device rotates or whenever a view controller is presented with the full-screen modal presentation style. Moreover, the supported orientations are retrieved only if this view controller returns YES from its shouldAutorotate method. [...] The system determines whether an orientation is supported by intersecting the value returned by the app’s supportedInterfaceOrientationsForWindow: method with the value returned by the supportedInterfaceOrientations method of the top-most full-screen controller.

所以我将 UINavigationController 子类化,为我的 MainNavigationController 提供了一个 bool 属性 landscapeOK 并使用它返回 supportedInterfaceOrientations< 中允许的方向。然后在我的每个 View Controller 的 viewWillAppear: 方法中,我都有这样一行

    [(MainNavigationController*)[self navigationController] setLandscapeOK:YES];

告诉我的 MainNavigationController 所需的行为。

问题来了:如果我现在以纵向模式导航到我的第四个 View 并将手机翻转过来,它会旋转到横向。现在我按下后退按钮返回到我的第三个 View ,它应该只能纵向工作。但它不会旋转回来。我如何让它这样做?

我试过了

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait]

在我的第三个 View Controller 的 viewWillAppear 方法中,但它什么也没做。这是错误的调用方法还是错误的调用位置,还是我应该以完全不同的方式实现整个过程?

最佳答案

我遇到了同样的问题,并找到了适合我的解决方案。 要使其工作,在您的 UINavigationController 中实现 - (NSUInteger)supportedInterfaceOrientations 是不够的。 您还需要在您的 Controller #3 中实现此方法,这是弹出 Controller #4 后第一个仅纵向的 Controller 。 所以,我的 UINavigationController 中有以下代码:

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    if (self.isLandscapeOK) {
        // for iPhone, you could also return UIInterfaceOrientationMaskAllButUpsideDown
        return UIInterfaceOrientationMaskAll;
    }
    return UIInterfaceOrientationMaskPortrait;
}

在 View Controller #3 中,添加以下内容:

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

您不需要向 View Controller #1、#2 和 #4 添加任何内容。 这对我有用,希望对您有所帮助。

关于iOS 6 : How do I restrict some views to portrait and allow others to rotate?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12630359/

相关文章:

iPhone使用iTunes传输文件,但不允许用户查看所有文档目录?

ios - @Published 用于计算属性(或最佳解决方法)

iphone - 语义问题 - Xcode 实现不完整

ios - Unity游戏无法在iPhone 4s上模拟

iphone - 验证,UITextField

ios - 自定义单元格 : unable to show data on main view

iphone - 通过避免应用程序 10 分钟时间限制在后台连续工作

ios - 如何实现委托(delegate)代替通知

ios - 使用 Flash CS5 开发的 iPhone 应用程序可以与外部服务器通信吗?

iphone - 开始和停止按钮,需要禁用和隐藏然后重新启用和可见