获取当前界面方向的不同方法?
据我所知,有3种方法可以获取当前界面方向:
self.interfaceOrientation
[[UIApplication sharedApplication] statusBarOrientation]
[[UIDevice currentDevice]orientation]< /code>
它们之间有什么区别?
There're 3 ways to get current interface orientation that I know of:
self.interfaceOrientation
[[UIApplication sharedApplication] statusBarOrientation]
[[UIDevice currentDevice] orientation]
What are the differences among them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
self.interfaceOrientation 返回 UIInterfaceOrientation,界面的当前方向。它是 UIViewController 中的一个属性,您只能在 UIViewController 类中访问该属性。
[[UIApplication sharedApplication] statusBarOrientation]
返回 UIInterfaceOrientation,即应用程序状态栏的当前方向。您可以在应用程序的任何点访问该属性。我的经验表明,这是检索真实界面方向的更有效方法。[[UIDevice currentDevice]orientation]
返回UIDeviceOrientation,设备方向。您可以在应用程序的任何点访问该属性。但请注意,UIDeviceOrientation 并不总是 UIInterfaceOrientation。例如,当您的设备位于普通桌面上时,您可能会收到意想不到的值。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.[[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.[[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.[[UIApplication sharedApplication] statusBarOrientation]
- 它始终等于四个值之一:纵向、向左横向、向右横向和颠倒纵向。[[UIDevice currentDevice]orientation]
- 该值等于标准的四个值,并且还:未知、面朝上或面朝下。[[UIApplication sharedApplication] statusBarOrientation]
- 是获取界面方向的更首选方法。[[UIApplication sharedApplication] statusBarOrientation]
- it always equals to one of four values: portrait, landscape left, landscape right and portrait upside down.[[UIDevice currentDevice] orientation]
- this one equals to standart four values and also: unknown, face up or face down.[[UIApplication sharedApplication] statusBarOrientation]
- is more prefered way to get interface orientation.从iOS 13开始
你可以使用窗口场景的interfaceOrientation属性
UIWindow.windowScene.interfaceOrientation
Since iOS 13
You can use the interfaceOrientation property of the window scene
UIWindow.windowScene.interfaceOrientation
所有视图控制器都有此功能
,或者您可以使用状态栏方向
适用于 iOS 9 :)
All view controllers have this function
or you can user the status bar orientation
Works in iOS 9 :)