iOS 7没有调用supportedInterfaceOrientations

我search了这个答案,但找不到解决我的问题的任何东西。

所以这里的问题是:我有一个自定义的UINavigationController,创build它时在rootViewController上调用supportedInterfaceOrientations方法(只支持肖像)。 但是当把另一个ViewController推入堆栈时,这个方法在被推送的ViewController上不会被调用(支持所有的倒置)。

我通过在viewDidLoad方法中调用[self supportedInterfaceOrientations]来解决它,但是我认为这不是解决问题的好方法。

我希望你能在这个问题上帮助我。

这是我在第二个viewController中实现的代码。

 - (BOOL)shouldAutorotate { return YES; } - (NSUInteger)supportedInterfaceOrientations { if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { [[[UIApplication sharedApplication] delegate] setGlobalOrientationMask:UIInterfaceOrientationMaskAllButUpsideDown]; return UIInterfaceOrientationMaskAllButUpsideDown; } else { [[[UIApplication sharedApplication] delegate] setGlobalOrientationMask:UIInterfaceOrientationMaskAll]; return UIInterfaceOrientationMaskAll; } } 

我认为johnma的解决scheme应该可以适用于大多数应用程序,但在我的情况下,我认为有一个特殊的问题,但我现在自己解决(不知道它是否是一个好的,但它的工作原理)。

我实现了- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated在我的navigationController委托的- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated方法。

 - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated { if (DEF_SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7")) { if ([viewController respondsToSelector:@selector(supportedInterfaceOrientations)]) { [viewController supportedInterfaceOrientations]; } } } 

我希望这可以帮助其他同样的问题。

您应该在您的自定义NavigationController中实现这些代码。

  - (NSUInteger)supportedInterfaceOrientations { if ([self.topViewController isMemberOfClass:[RootViewController class]]){ return UIInterfaceOrientationMaskPortrait; }else{ return UIInterfaceOrientationMaskAllButUpsideDown; } } 

因为当你的设备旋转时,它会先问你的app rootController(Custom NavigationController),如果supportedInterfaceOrientations没有在那里实现。 那么它会询问rootController的supportedInterfaceOrientations

作为主窗口的根视图控制器或在主窗口上全屏显示的视图控制器可以声明它支持的方向。 视图控制器编程指南

这在iOS 8中工作正常。

但在iOS 9(Xcode 7)中,我变成了一个警告:“在'supportedInterfaceOrientations'实现中相互冲突的返回types:'UIInterfaceOrientationMask'(aka'enum UIInterfaceOrientationMask')vs'NSUInteger(aka'unsigned long')”

我已经改变了:

 -(UIInterfaceOrientationMask)supportedInterfaceOrientations{ return UIInterfaceOrientationMaskPortrait; } 

所选答案的Swift版本:

 override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { if self.topViewController is ViewController { return .Portrait } else { return .All } }