仅 ONE VIEW 横向模式

问题描述:

我完成了我的 iOS 应用,但我只需要将一个视图设置为横向模式,其余的视图只能在纵向模式下看到.

I finished my iOS app but I need to set only ONE view to landscape mode, the rest of the views can only be seen in portrait mode.

我使用的是 Xcode 5.1,我通过从右侧面板中放入我的故事板视图控制器来创建我的所有视图,所以如果您要告诉我在某处编写一些代码,请准确告诉我我需要在哪里写吧.

I'm using Xcode 5.1 and I created all of my Views by dropping in my storyboard View Controllers from the right panel, so if you are going to tell me to write some code somewhere, please tell me exactly where I need to write it.

我在这里阅读了一个解决方案 UINavigationController Force Rotate 但我不知道在哪里写代码.我需要手动创建一个 UIViewController 吗?

I read one solution here UINavigationController Force Rotate but I don't know where to write that code. Do I need to create one UIViewController manually?

我假设您在这里针对的是 iOS 7(使用 XCode 5.1,我认为我是对的).

I am gonna suppose you are targeting iOS 7 here (using XCode 5.1, I think I am right).

首先,您必须了解,为了仅打开 40 多个横向视图中的一个视图,您的应用应该允许横向和纵向界面方向.默认情况下是这种情况,但您可以在目标的设置、General 选项卡、Deployment Info 部分(见下面的屏幕截图)中检查它.

First, you have to understand that in order to open even just one view out of over 40 in landscape, your app should allow both landscape and portrait interface orientations. It is the case by default, but you can check it in your target's settings, General tab, Deployment Info section (see screenshot below).

然后,因为您允许整个应用程序同时使用横向和纵向,所以您必须告诉每个仅限纵向的 UIViewController 它不应自动旋转,并添加此方法的实现:

Then, because you allowed both landscape and portrait for the entire app, you will have to tell every portrait-only UIViewController that it should not autorotate, adding this method's implementation:

- (BOOL)shouldAutorotate {
  return NO;
}

最后,对于您特定的仅限横向的控制器,并且因为您说过要以模态方式呈现它,所以您可以实现以下方法:

Finally, for your specific landscape-only controller, and because you said you are presenting it modally, you can just implement these methods:

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
  return UIInterfaceOrientationLandscapeLeft; // or Right of course
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  return UIInterfaceOrientationMaskLandscape;
}

希望这会有所帮助,