ios 获取当前界面方向的不同方式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7968451/
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
Different ways of getting current interface orientation?
提问by Anh Do
There're 3 ways to get current interface orientation that I know of:
我知道有 3 种方法可以获得当前的界面方向:
self.interfaceOrientation
[[UIApplication sharedApplication] statusBarOrientation]
[[UIDevice currentDevice] orientation]
self.interfaceOrientation
[[UIApplication sharedApplication] statusBarOrientation]
[[UIDevice currentDevice] orientation]
What are the differences among them?
它们之间有什么区别?
回答by beryllium
self.interfaceOrientation
returns UIInterfaceOrientation, current orientation of the interface. It is a property in UIViewController, you can access to this one only in UIViewController classes.
self.interfaceOrientation
返回 UIInterfaceOrientation,界面的当前方向。它是 UIViewController 中的一个属性,您只能在 UIViewController 类中访问该属性。
[[UIApplication sharedApplication] statusBarOrientation]
returns UIInterfaceOrientation, current orientation of the application's status bar. You can access to that property in any point of your application. My experience shows that it's more effective way to retrieve real interface orientation.
[[UIApplication sharedApplication] statusBarOrientation]
返回 UIInterfaceOrientation,应用程序状态栏的当前方向。您可以在应用程序的任何位置访问该属性。我的经验表明,这是检索真实界面方向的更有效方法。
[[UIDevice currentDevice] orientation]
returns UIDeviceOrientation, device orientation. You can access to that property in any point of your application. But note that UIDeviceOrientation is not always UIInterfaceOrientation. For example, when your device is on a plain table you can receive unexpected value.
[[UIDevice currentDevice] orientation]
返回UIDeviceOrientation,设备方向。您可以在应用程序的任何位置访问该属性。但请注意,UIDeviceOrientation 并不总是 UIInterfaceOrientation。例如,当您的设备在普通桌子上时,您可能会收到意外的值。
回答by holodnyalex
[[UIApplication sharedApplication] statusBarOrientation]
- it always equals to one of four values: portrait, landscape left, landscape right and portrait upside down.
[[UIApplication sharedApplication] statusBarOrientation]
- 它总是等于四个值之一:纵向、横向左侧、横向右侧和纵向颠倒。
[[UIDevice currentDevice] orientation]
- this one equals to standart four values and also: unknown, face up or face down.
[[UIDevice currentDevice] orientation]
- 这一个等于标准的四个值,还有:未知、面朝上或面朝下。
[[UIApplication sharedApplication] statusBarOrientation]
- is more prefered way to get interface orientation.
[[UIApplication sharedApplication] statusBarOrientation]
- 是获得界面方向的更优选方式。
回答by Eduardo Irias
All view controllers have this function
所有视图控制器都有这个功能
override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
//fromInterfaceOrientation.isLandscape
}
or you can user the status bar orientation
或者您可以使用状态栏方向
if UIApplication.sharedApplication().statusBarOrientation == UIInterfaceOrientation.Portrait {
//
}
Works in iOS 9 :)
适用于 iOS 9 :)
回答by Паша Матюхин
Since iOS 13
从 iOS 13 开始
You can use the interfaceOrientation property of the window scene
您可以使用窗口场景的 interfaceOrientation 属性
UIWindow.windowScene.interfaceOrientation
UIWindow.windowScene.interfaceOrientation
let interfaceOrientation = UIApplication.shared.windows.first?.windowScene?.interfaceOrientation ?? UIInterfaceOrientation.unknown