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

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

问题描述

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

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天全站免登陆