获取当前界面方向的不同方法?

发布于 2024-12-13 14:41:37 字数 263 浏览 0 评论 0原文

据我所知,有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

软糖 2024-12-20 14:41:37

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.

血之狂魔 2024-12-20 14:41:37

[[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.

旧瑾黎汐 2024-12-20 14:41:37

从iOS 13开始

你可以使用窗口场景的interfaceOrientation属性

UIWindow.windowScene.interfaceOrientation

let interfaceOrientation = UIApplication.shared.windows.first?.windowScene?.interfaceOrientation ?? UIInterfaceOrientation.unknown

Since iOS 13

You can use the interfaceOrientation property of the window scene

UIWindow.windowScene.interfaceOrientation

let interfaceOrientation = UIApplication.shared.windows.first?.windowScene?.interfaceOrientation ?? UIInterfaceOrientation.unknown
毅然前行 2024-12-20 14:41:37

所有视图控制器都有此功能

   override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
          //fromInterfaceOrientation.isLandscape
   }

,或者您可以使用状态栏方向

   if UIApplication.sharedApplication().statusBarOrientation == UIInterfaceOrientation.Portrait {
        //
   }

适用于 iOS 9 :)

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文