iOS 6:如何将某些视图限制为纵向并允许其他视图旋转? [英] iOS 6: How do I restrict some views to portrait and allow others to rotate?

查看:15
本文介绍了iOS 6:如何将某些视图限制为纵向并允许其他视图旋转?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an iPhone app that uses a UINavigationController to present a drill-down interface: First one view, then another, up to four levels deep. I want the first three views restricted to portrait orientation and only the last view should be allowed to rotate to landscape. When returning from the fourth view to the third and the fourth view was in landscape orientation I want everything to rotate back to portrait.

在 iOS 5 中,我只是在我的每个视图控制器中定义了 shouldAutorotateToInterfaceOrientation: 来为允许的方向返回 YES.一切都按上述方式工作,包括从视图控制器 #4 返回到 #3 时,即使设备处于横向,也返回到纵向.

In iOS 5 I simply defined shouldAutorotateToInterfaceOrientation: in each of my view controllers to return YES for the allowable orientations. Everything worked as described above, including the return to portrait even if the device was being held in landscape orientation when returning from view controller #4 to #3.

在 iOS 6 中,所有的视图控制器都旋转到横向,打破了那些不想要的.iOS 6 发行说明说

In iOS 6 all view controllers rotate to landscape, breaking those that weren't meant to. The iOS 6 release notes say

更多的责任正在转移到应用程序和应用程序委托上.现在,iOS 容器(例如 UINavigationController)不会咨询他们的孩子来确定他们是否应该自动旋转.[...] 每当设备旋转或视图控制器以全屏模式呈现样式呈现时,系统都会向最顶层的全屏视图控制器(通常是根视图控制器)询问其支持的界面方向.此外,仅当此视图控制器从其 shouldAutorotate 方法返回 YES 时,才会检索支持的方向.[...] 系统通过将应用程序的 supportedInterfaceOrientationsForWindow: 方法返回的值与顶部的 supportedInterfaceOrientations 方法返回的值相交来确定是否支持某个方向.最全屏的控制器.

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 一个布尔属性 landscapeOK 并用它来返回 supportedInterfaceOrientations<中允许的方向/代码>.然后在我的每个视图控制器的 viewWillAppear: 方法中,我都有这样一行

So I subclassed UINavigationController, gave my MainNavigationController a boolean property landscapeOK and used this to return the allowable orientations in supportedInterfaceOrientations. Then in each of my view controllers' viewWillAppear: methods I have a line like this

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

告诉我的 MainNavigationController 所需的行为.

to tell my MainNavigationController the desired behavior.

问题来了:如果我现在以纵向模式导航到我的第四个视图并将手机翻转过来,它会旋转到横向.现在我按下后退按钮返回到我的第三个视图,它应该只适用于纵向.但它不会旋转回来.我如何让它做到这一点?

Here comes the question: If I now navigate to my fourth view in portrait mode and turn the phone over it rotates to landscape. Now I press the back button to return to my third view which is supposed to work portrait only. But it doesn't rotate back. How do I make it do that?

我试过了

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait]

在我的第三个视图控制器的 viewWillAppear 方法中,但它没有做任何事情.这是调用错误的方法还是错误的调用位置,还是我应该以完全不同的方式实现整个事情?

in the viewWillAppear method of my third view controller, but it doesn't do anything. Is this the wrong method to call or maybe the wrong place to call it or should I be implementing the whole thing in a totally different way?

推荐答案

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

I had the same problem and found a solution that works for me. To make it work, it is not sufficient to implement - (NSUInteger)supportedInterfaceOrientations in your UINavigationController. You also need to implement this method in your controller #3, which is the first one to be portrait-only after popping controller #4. So, I have the following code in my UINavigationController:

- (BOOL)shouldAutorotate
{
    return YES;
}

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

在视图控制器 #3 中,添加以下内容:

In view controller #3, add the following:

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

您不需要向视图控制器 #1、#2 和 #4 添加任何内容.这对我有用,希望能帮到你.

You don't need to add anything to your view controllers #1, #2, and #4. This works for me, I hope it will help you.

这篇关于iOS 6:如何将某些视图限制为纵向并允许其他视图旋转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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