ios 如何禁用横向?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11785565/
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-08-30 19:34:01  来源:igfitidea点击:

How can I disable landscape orientation?

iphoneobjective-ciosxcodeorientation

提问by user1483652

I am making an iPhone app and I need it to be in portrait mode, so if the user moves the device sideways, it does not automatically rotate. How can I do this?

我正在制作一个 iPhone 应用程序,我需要它处于纵向模式,所以如果用户侧向移动设备,它不会自动旋转。我怎样才能做到这一点?

回答by thegrinner

To disable orientations for a particular View Controller, you should now override supportedInterfaceOrientationsand preferredInterfaceOrientationForPresentation.

要禁用特定视图控制器的方向,您现在应该覆盖supportedInterfaceOrientationspreferredInterfaceOrientationForPresentation

- (NSUInteger) supportedInterfaceOrientations {
    // Return a bitmask of supported orientations. If you need more,
    // use bitwise or (see the commented return).
    return UIInterfaceOrientationMaskPortrait;
    // return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}

- (UIInterfaceOrientation) preferredInterfaceOrientationForPresentation {
    // Return the orientation you'd prefer - this is what it launches to. The
    // user can still rotate. You don't have to implement this method, in which
    // case it launches in the current orientation
    return UIInterfaceOrientationPortrait;
}

If you're targeting something older than iOS 6, you want the shouldAutorotateToInterfaceOrientation:method. By changing when it returns yes, you'll determine if it will rotate to said orientation. This will only allow the normal portrait orientation.

如果你的目标是比 iOS 6 更旧的东西,你需要这个shouldAutorotateToInterfaceOrientation:方法。通过更改返回 yes 的时间,您将确定它是否会旋转到所述方向。这将只允许正常的纵向。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 

    // Use this to allow upside down as well
    //return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}

Note that shouldAutorotateToInterfaceOrientation:has been deprecated in iOS 6.0.

请注意,shouldAutorotateToInterfaceOrientation:已在 iOS 6.0 中弃用

回答by skywinder

Xcode 5 and above

Xcode 5 及以上

  • Click your project in the Project Navigator in the left sidebar to open the project settings
  • Go to the Generaltab.
  • Uncheck the options you don't want in the Deployment Infosection, under Device Orientation
  • 在左侧边栏中的项目导航器中单击您的项目以打开项目设置
  • 转到常规选项卡。
  • 在“设备方向”下的“部署信息”部分取消选中您不需要的选项

screenshot showing where to click

显示点击位置的屏幕截图

回答by Thomas Verbeek

Xcode 4 and below

Xcode 4 及以下

For those who missed it: you can use the project settings screen to fix orientations throughout the app (no need to override methods in every controller):

对于那些错过它的人:您可以使用项目设置屏幕来修复整个应用程序的方向(无需覆盖每个控制器中的方法):

enter image description here

在此处输入图片说明

It's as simple as toggling the supported interface orientations. You can find by clicking on your Project in the left panel > the app target > Summary tab.

就像切换支持的界面方向一样简单。您可以通过单击左侧面板中的项目 > 应用程序目标 > 摘要选项卡来找到。

回答by iOS

If you want to disable landscape orientation for both iPhone and iPad.

如果您想为iPhone 和 iPad禁用横向。

Go to Targetsand Go to the General tab. See the below screen and deselect landscape left and landscape right.

转到目标并转到常规选项卡。请参阅下面的屏幕并取消选择横向向左和向右横向

enter image description here

在此处输入图片说明

Here in this case only iPhone landscape mode will be disabled not for iPad. For iPad all modes are anabled. If you want select device option from Universal to iPad. It will looks like this. See below screen.

在这种情况下,只会禁用 iPhone 横向模式,而不是 iPad。对于 iPad,所有模式都已启用。如果您想从通用到 iPad 中选择设备选项。它看起来像这样。请参阅下面的屏幕。

enter image description here

在此处输入图片说明

Now you need to deselect all modes except Portraitfor iPad. See below screenshot.

现在您需要取消选择iPad人像以外的所有模式。请看下面的截图。

enter image description here

在此处输入图片说明

Now you successfully disabled all modes except Portraitfor all devices.

现在您已成功为所有设备禁用除纵向以外的所有模式。

回答by kurtanamo

Swift 3If you have a navigationController, subclass it like this (for portrait only):

Swift 3如果你有一个导航控制器,像这样子类化它(仅适用于纵向):

class CustomNavigationViewController: UINavigationController {

  override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.portrait
  }

  override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
    return UIInterfaceOrientation.portrait
  }
}

回答by midhun p

Xcode 8, Xcode 9, Xcode 10 and above

Xcode 8、Xcode 9、Xcode 10 及更高版本

enter image description here

在此处输入图片说明

回答by Anonimys

Most simple solution separate for iPhone and iPad (Universal) - its remove unnecessary orientation in the info.plistfile or Project -> Info -> Custom iOS Target Properties.

适用于 iPhone 和 iPad(通用)的最简单的解决方案 - 它删除了info.plist文件或Project -> Info -> Custom iOS Target Properties中不必要的方向。

Just add or remove orientation item from list:

只需从列表中添加或删除方向项:

  • Supported interface orientationfor iPhone
  • Supported interface orientations (iPad)for iPad
  • 支持的接口方向iPhone
  • 支持的接口的取向总览ipad公司

enter image description here

在此处输入图片说明

回答by ElasticThoughts

Removing the method shouldAutorotateToInterfaceOrientationfrom your class entirely also works. If you don't plan on rotating then it makes no sense to have the method in your class, the less code the better, keeps things clean.

shouldAutorotateToInterfaceOrientation从您的类中完全删除该方法也有效。如果你不打算再转动它是没有意义的在你的类中的方法,代码越少就越好,保持干净的东西。