Different ways of getting current interface orientation?

﹥>﹥吖頭↗ 提交于 2019-12-18 10:08:18

问题


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?


回答1:


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.




回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/7968451/different-ways-of-getting-current-interface-orientation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!