Different ways to get the current orientation of the interface?

There are 3 ways to get the current orientation of the interface that I know of:

  • self.interfaceOrientation
  • [[UIApplication sharedApplication] statusBarOrientation]
  • [[UIDevice currentDevice] orientation]

What are the differences between the two?

+62
ios orientation
Nov 01. 2018-11-11T00:
source share
3 answers

self.interfaceOrientation returns the UIInterfaceOrientation, the current orientation of the interface. This property is in the UIViewController, you can only access this in the UIViewController classes.

[[UIApplication sharedApplication] statusBarOrientation] returns the UIInterfaceOrientation, the current orientation of the application status bar. You can access this property anywhere in your application. My experience shows that this is a more efficient way to get a real interface orientation.

[[UIDevice currentDevice] orientation] returns the UIDeviceOrientation device orientation. You can access this property anywhere in your application. But note that UIDeviceOrientation is not always UIInterfaceOrientation. For example, when your device is on a simple table, you may get an unexpected value.

+110
Nov 03 '11 at 8:29
source share

[[UIApplication sharedApplication] statusBarOrientation] - it is always equal to one of four values: portrait, landscape on the left, landscape on the right and portrait upside down.

[[UIDevice currentDevice] orientation] - this indicator is equal to the standard four values, and also: it is not known, up or down.

[[UIApplication sharedApplication] statusBarOrientation] - a more preferable way to get the orientation of the interface.

+23
Nov 03 '11 at 6:57
source share

All view controllers have this feature.

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

or you can specify the orientation of the status bar

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

Works in iOS 9 :)

+1
Sep 28 '15 at 4:50
source share



All Articles