iOS 6 ViewController正在旋转,但不应该

Nic*_*sla 8 uiviewcontroller ios6 xcode4.5

我希望我的几个应用程序视图控制器不在iOS 6.0中旋转.这就是我在iOS 6中进行旋转所做的事情:

1.)在应用程序中设置windows rootviewController:didFinishLaunchingWithOptions:

self.window.rootViewController = self.tabBarController;
Run Code Online (Sandbox Code Playgroud)

2.)在我的目标(在XCode中)中设置"支持的接口方向",以便我可以使用所有方向

3.)实现了新的iOS 6.0旋转功能

- (BOOL) shouldAutorotate {

    return YES;
}


-(NSUInteger)supportedInterfaceOrientations{

    return UIInterfaceOrientationMaskAll;
}
Run Code Online (Sandbox Code Playgroud)

4.)由于某些原因,我将UINavigationController子类化并实现了这些新功能并使用了这个新的NavigationController而不是原始的.

到目前为止一切顺利,所有视图控制器现在都可以旋转到每个方向.现在我想要几个viewController不旋转,只留在肖像.但是当我在这样的特定视图控制器中设置新的旋转方法时,它仍然会旋转到每个方向:

- (BOOL) shouldAutorotate {

    return NO;
}


-(NSUInteger)supportedInterfaceOrientations{

    return UIInterfaceOrientationMaskPortrait;
}
Run Code Online (Sandbox Code Playgroud)

同样像上面那样设置navigationController的旋转功能不会改变任何东西.(所有视图控制器都可以旋转到每个方向)

我究竟做错了什么?

编辑:

设置首选的Interfaceorientation也不会改变任何内容:

- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation {

    return UIInterfaceOrientationMaskPortrait;
}
Run Code Online (Sandbox Code Playgroud)

Ant*_*ony 11

如果您希望我们的所有导航控制器都尊重顶视图控制器,您可以使用类别.我发现它比继承更容易.

@implementation UINavigationController (Rotation_IOS6)

-(BOOL)shouldAutorotate
{
    return [[self.viewControllers lastObject] shouldAutorotate];
}

-(NSUInteger)supportedInterfaceOrientations
{
    return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}

@end
Run Code Online (Sandbox Code Playgroud)

  • 小心这个解决方案 - 如果视图控制器的supportedInterfaceOrientations方法碰巧在nav控制器上调用supportedInterfaceOrientations,那么你有无限的递归.我发现Apple的UIPrintingProgressViewController要求导航控制器提供supportedInterfaceOrientations的困难方法...... (5认同)