supportedInterfaceOrientations未使用iOS 7调用

Man*_* Wa 4 objective-c orientation uinavigationcontroller ios ios7

我搜索了一个答案,但找不到任何解决我问题的方法.

所以这就是问题所在:我有一个自定义的UINavigationController,在创建它时supportedInterfaceOrientations,在rootViewController上调用该方法(仅支持肖像).但是当将其他ViewController推送到堆栈时,不会在推送的ViewController上调用此方法(支持除了倒挂之外的所有方法).

我解决它通过调用[self supportedInterfaceOrientations]viewDidLoad-方法,但我认为这不是解决问题的好办法.

我希望你能帮助我解决这个问题.

这是我在第二个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;
    }
}
Run Code Online (Sandbox Code Playgroud)

我认为来自johnMa的解决方案应该适用于大多数应用程序,但在我的情况下,我认为有一个特殊的问题,但我现在自己解决了(不确定它是否是一个好的,但它的工作原理).

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated在navigationController-delegate上实现了该方法.

- (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];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望这可以帮助其他人解决同样的问题.

joh*_*nMa 13

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

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

因为当你的设备旋转时,它会首先询问你的应用程序rootController(Custom NavigationController),如果supportedInterfaceOrientations没有在那里实现的话.那么它会询问rootController supportedInterfaceOrientations.

视图控制器充当主窗口的根视图控制器或在主窗口上全屏显示,可以声明它支持的方向.View Controller编程指南