如何在 Xcode 中管理屏幕方向?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14912388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:47:08  来源:igfitidea点击:

How to manage screen orientation in Xcode?

iphoneiosxcode

提问by Night Hunter

I have two view controller in my app.
1st view controller is portrait mode and 2nd view controller in landscape mode.

我的应用程序中有两个视图控制器。
第一个视图控制器是纵向模式,第二个视图控制器是横向模式。

I don't want to change their orientation even when i rotate my iPhone. How can i make?

即使我旋转 iPhone,我也不想改变它们的方向。我该怎么做?

回答by dasblinkenlight

In the Xcode, highlight the project in the Project Navigator, and select the target in the project tree. Open the Summary page, and go to the Supported Interface Orientations section. Un-click the orientations that you do not want your application to support.

在 Xcode 中,在 Project Navigator 中突出显示项目,然后在项目树中选择目标。打开“摘要”页面,然后转到“支持的接口方向”部分。取消单击您不希望应用程序支持的方向。

In the story board, choose your first view controller, go to the Attributes inspector, and set orientation to "Portrait" in the Simulated Metrics section. Now choose the second view controller, and set its orientation to "landscape".

在故事板中,选择您的第一个视图控制器,转到 Attributes 检查器,然后在 Simulated Metrics 部分将方向设置为“Portrait”。现在选择第二个视图控制器,并将其方向设置为“横向”。

In the view controller code, implement

在视图控制器代码中,实现

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

for the first view controller, and

对于第一个视图控制器,以及

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

for the second view controller. This should fix the problem for you.

对于第二个视图控制器。这应该为您解决问题。

回答by βhargav?

In your case you have two VCs. So in both VC under below method just handle you orientations. and perform check against toInterfaceOrientation and return YES.

在您的情况下,您有两个 VC。因此,在下面的两个 VC 方法中,只需处理您的方向。并针对 toInterfaceOrientation 执行检查并返回 YES。

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
     // For your VC1
     if (toInterfaceOrientation == UIInterfaceOrientationPortrait)
           return YES;
     // For your VC2
     /*
     if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
           return YES;  
     */

}