简体   繁体   English

用界面方向替换设备方向

[英]Replace device orientation with interface orientation

I have been using this for orientation changes 我一直在使用它来改变方向

  if (UIDevice.current.orientation == UIDeviceOrientation.portrait)

How do I replace this by UIInterfaceOrientation.portrait in the above syntax. 如何在上面的语法中通过UIInterfaceOrientation.portrait替换它。 This is because UIDeviceOreientation has faceup and facedown , which are of no use to me. 这是因为UIDeviceOreientation具有faceupfacedown ,这对我没用。

您可以通过以下行替换它

if (UIApplication.shared.statusBarOrientation == UIInterfaceOrientation.portrait)

Trungduc's answer will work but as Apple states in their documentation : Trungduc的答案将有效,但正如Apple在其文档中所述

Starting in iOS 8, you should employ the UITraitCollection and UITraitEnvironment APIs, and size class properties as used in those APIs, instead of using UIInterfaceOrientation constants or otherwise writing your app in terms of interface orientation. 从iOS 8开始,您应该使用UITraitCollection和UITraitEnvironment API,以及这些API中使用的大小类属性,而不是使用UIInterfaceOrientation常量或以接口方向编写应用程序。

Edit: if you don't want to get into the flexibility UITraitCollection gives you you can keep your method but simplify it: 编辑:如果你不想进入灵活性UITraitCollection给你,你可以保留你的方法,但简化它:

if (UIDevice.current.orientation.isPortrait) {
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM