Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 获得当前界面方向的不同方法?_Ios_Orientation - Fatal编程技术网

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

Ios 获得当前界面方向的不同方法?,ios,orientation,Ios,Orientation,我知道有3种方法可以获得当前界面方向: self.interfaceOrientation [[UIApplication sharedApplication]statusBarOrientation] [[UIDevice currentDevice]方向] 它们之间的区别是什么?[[UIApplication sharedApplication]statusBarOrientation]-它始终等于四个值之一:纵向、横向左侧、横向右侧和纵向倒置 [[UIDevice currentDev

我知道有3种方法可以获得当前界面方向:


  • self.interfaceOrientation
  • [[UIApplication sharedApplication]statusBarOrientation]
  • [[UIDevice currentDevice]方向]

它们之间的区别是什么?

[[UIApplication sharedApplication]statusBarOrientation]
-它始终等于四个值之一:纵向、横向左侧、横向右侧和纵向倒置

[[UIDevice currentDevice]方向]
-此值等于标准的四个值,也等于:未知、正面朝上或正面朝下


[[UIApplication sharedApplication]statusBarOrientation]
-是获取接口方向的更首选方式。

self.interfaceOrientation
返回UIInterfaceOrientation,即接口的当前方向。它是UIViewController中的一个属性,您只能在UIViewController类中访问该属性


[[UIApplication sharedApplication]statusBarOrientation]
返回UIInterfaceOrientation,即应用程序状态栏的当前方向。您可以在应用程序的任何位置访问该属性。我的经验表明,这是一种更有效的方法来检索真正的界面方向


[[UIDevice currentDevice]方向]
返回UIDeviceOrientation,设备方向。您可以在应用程序的任何位置访问该属性。但请注意,UIDeviceOrientation并不总是UIInterfaceOrientation。例如,当您的设备位于普通表格上时,您可能会收到意外值。

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

   override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
          //fromInterfaceOrientation.isLandscape
   }
也可以使用状态栏方向

   if UIApplication.sharedApplication().statusBarOrientation == UIInterfaceOrientation.Portrait {
        //
   }
适用于iOS 9:)

自iOS 13以来

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

UIWindow.windowScene.interfaceOrientation

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

还有一个关于界面方向的答案-@Pwner设备的方向不一定是UI的方向。例如,如果您的应用程序只支持横向,而您将设备转向纵向,则设备方向会显示为纵向,但用户界面的方向仍然是横向(并且也将保持横向)。此外,如果设备是平放在桌子上的,它无法确定其方向是什么,但它可以确定UI在任何时候的方向。只是一个更正,当设备位于平放的桌子上时,这不是一个“意外值”。UIDeviceOrientation共有7个值,如UIDevice.h中声明的,其中包括UIDeviceOrientationUnknown、UIDeviceOrientationFaceUp和UIDeviceOrientationFaceDown。您可以记录deviceOrientation值,并查看faceUp或faceDown的值是否等于5或6。
self.interfaceOrientation
在iOS 8中不受欢迎。iOS通知中心和iOS控制中心也会影响
[[UIDevice currentDevice]方向]
值。例如,您的应用程序仅支持纵向模式。在这种情况下,您将从左侧和右侧(而不是从顶部和底部)看到通知中心面板和控制中心面板。在这种情况下,当您的设备真正处于横向时,您将在
[[UIDevice currentDevice]方向]
中获得纵向值。此外,
[[UIDevice currentDevice]方向]
“…始终返回0,除非通过调用BegingeratingDeviceOrientationNotifications启用了方向通知。”(文档)12例偏头痛和5例溃疡之后,我可以证实
[[UIApplication sharedApplication]statusBarOrientation]
是一种更可靠、更简单的检测和管理方向变化的方法。