首页 文章

除模态视图控制器外的所有视图控制器中的纵向方向

提问于
浏览
4

我正在使用代码呈现模态视图:

[self presentViewController:movieViewController animated:YES completion:^{
     // completed
 }];

在movieViewController中,控制器被解雇:

[self dismissViewControllerAnimated:YES completion:^{
    // back to previous view controller
}];

目前,我的所有视图控制器都可以纵向和两个横向方向查看 .

除了模态视图控制器之外,如何将所有视图控制器限制为纵向?因此,可以在三个方向中看到模态视图控制器,而在一个方向中可以看到其他所有内容 .

1 回答

  • 20

    在appDelegate中:

    - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
            return UIInterfaceOrientationMaskAllButUpsideDown;
        }
    

    特殊的UINavigationController子类(如果使用导航控制器)与方法:

    - (NSUInteger)supportedInterfaceOrientations {
        if (self.topViewController.presentedViewController) {
            return self.topViewController.presentedViewController.supportedInterfaceOrientations;
        }
        return self.topViewController.supportedInterfaceOrientations;
    }
    
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
        return self.topViewController.preferredInterfaceOrientationForPresentation;
    }
    

    每个视图控制器应该返回它自己支持的方向和首选的显示方向:

    - (NSUInteger)supportedInterfaceOrientations {
        return UIInterfaceOrientationMaskPortrait;
    }
    
    - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
        return UIInterfaceOrientationPortrait;
    }
    

    我的应用程序以纵向工作,但视频播放器以模态vc打开:

    - (NSUInteger)supportedInterfaceOrientations {
         return UIInterfaceOrientationMaskAllButUpsideDown;
     }
    
     - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
         return UIInterfaceOrientationLandscapeLeft;
     }
    

    它对我来说很完美,希望它有所帮助!

相关问题