xcode 在 iOS6 中未调用 shouldAutoRotate 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13588325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:11:53  来源:igfitidea点击:

shouldAutoRotate Method Not Called in iOS6

iphonexcodeios5rotationios6

提问by GuybrushThreepwood

I have a UIViewControllerdetail view which is pushed from a UITableViewin a UINavigationController. In the UIViewControllerI add a number of subviews (e.g a UITextView, UIImageView).

我有一个UIViewController从 a UITableViewin a推送的详细视图UINavigationController。在UIViewControllerI 添加多个子视图(例如 a UITextView, UIImageView)。

In iOS5I used this code to stop autorotation if my picture view was enlarged :

iOS5我用这个代码,以停止自转,如果我的图片视图被扩大了:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
if (scrollView.isZoomed) {
    return NO;
}
else {
    return YES;
}

}

}

I am trying to achieve the same thing under iOS6using :

我试图在iOS6using下实现同样的事情:

- (BOOL)shouldAutorotate {
return FALSE;
}

However this method is never called and the app continues rotating.

然而,这个方法永远不会被调用,应用程序会继续旋转。

Can anyone help ?

任何人都可以帮忙吗?

采纳答案by alemangui

If you have a Navigation Controller managing these views, the shouldAutorotatemethod won't be called. You would have to subclass UINavigationControllerand override methods shouldAutorotateand supportedIntervalOrientations.

如果您有一个 Navigation Controller 管理这些视图,则不会调用shouldAutorotate方法。您必须继承 UINavigationController并覆盖方法shouldAutorotatesupportedIntervalOrientations

From the docs:

从文档:

Now, iOS containers (such as UINavigationController) do not consult their children to determine whether they should autorotate

现在,iOS 容器(例如 UINavigationController)不会咨询他们的孩子来确定他们是否应该自动旋转

Edit-----

编辑 - - -

As mentioned below by Lomax, subclassing UINavigationController is discouraged by Apple. You should try a category instead (this SO question explains it well):

正如 Lomax 在下面提到的,Apple 不鼓励子类化 UINavigationController。你应该尝试一个类别(这个 SO question 很好地解释了它):

@implementation UINavigationController 
-(BOOL)shouldAutorotate
{
    // your code
}

-(NSUInteger)supportedInterfaceOrientations
{
    (...)
}

@end