iOS 6.0限制导航控制器内的自动旋转?

时间:2012-10-18 11:58:45

标签: ios uiviewcontroller uinavigationcontroller uiinterfaceorientation autorotate

我还应该做些什么?

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

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

-(BOOL)shouldAutoRotate
{
    return NO;
}

我的viewController仍在旋转。


它嵌入在导航堆栈中。 如果我是UINavigationController的子类,并在那里实现相同的只有纵向模板,并且我将viewController嵌入到调整后的navigationController中,而不是它的工作原理,但我无意在UINavigationController出现的任何地方重写我的代码。

这里的最佳做法是什么?

2 个答案:

答案 0 :(得分:6)

ORIGINAL ANSWER:不需要子类 - 只需像我在我的解决方案中描述的那样做一个类别: Top-home button portrait orientation in iOS6 simulator not working

基本上,对于iPhone,UINavigationController允许旋转除“顶部主页按钮肖像”之外的所有内容,对于iPad,它允许所有内容。

因此,要么将类别转发给当前活动的视图控制器,要么将其转换为静态的类似

的UINavigationController-Rotation.h:

@interface UINavigationController (Rotation)
@end

的UINavigationController-Rotation.m:

#import "UINavigationController-Rotation.h"

@implementation UINavigationController (Rotation)

#pragma From UINavigationController

- (BOOL)shouldAutorotate {

    return NO;
}

- (NSUInteger)supportedInterfaceOrientations {

    return UIInterfaceOrientationMaskPortrait;
}

#pragma -

@end

更新:正如Javier Soto指出的那样,如果第二个类别做同样的事情,这可能会导致未定义的行为。在这种情况下,子类化可能是更好的解决方案。

在你知道没有其他类别做同样事情的情况下,我仍然认为这是一个有效,省力,本地和实用的解决方案。我不是那么虔诚的。决定你自己。

答案 1 :(得分:3)

您应该从UINavigationController继承并在任何地方使用自定义的。这不是那么多工作(只是在代码中搜索UINavigationController的出现次数)。这将变得更加灵活,因为如果有必要,您将能够自定义其他内容。

从不在覆盖主类中方法的类别中执行此操作,就像其他响应所示。