除了模态视图控制器外,所有视图控制器都需以纵向方向显示

6

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

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

在movieViewController内部,控制器可以通过以下方式关闭:

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

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

我该如何限制所有视图控制器仅为纵向,除了模态视图控制器?因此,模态视图控制器可以在三个方向中查看,而其他一切只能在一个方向中查看。


你打算使用iOS 6还是5? - iiFreeman
1个回答

21

在 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;
}

我的应用程序只支持竖屏,但视频播放器会以模态视图控制器的形式打开:

 - (NSUInteger)supportedInterfaceOrientations {
     return UIInterfaceOrientationMaskAllButUpsideDown;
 }

 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
     return UIInterfaceOrientationLandscapeLeft;
 }

它对我来说完美地工作,希望能帮到你!


我不得不在我的视频播放器的supportedInterfaceOrientations中使用UIInterfaceOrientationMaskLandscapeLeft来强制横屏,以使其正常工作。否则我会遇到崩溃问题。但是视频本来就是要横屏观看的,所以没有问题。 - cannyboy
不,你需要将所有支持的方向作为掩码返回,并返回一个横向方向作为首选呈现方式。 - iiFreeman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接