68

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?

kmiklas
  • 13,085
  • 22
  • 67
  • 103
Anh
  • 6,523
  • 7
  • 46
  • 59

4 Answers4

114

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.

beryllium
  • 29,669
  • 15
  • 106
  • 125
  • 1
    one more answer regarding interface orientation - http://stackoverflow.com/a/3897243/194544 – beryllium Nov 06 '12 at 18:42
  • 3
    @Pwner The orientation of the device is not necessary the orientation of the UI. E.g. if your app only supports landscape orientation and you turn the device to portrait orientation, the device orientation will say it's portrait, but then orientation of the UI is still landscape (and it will also stay landscape). Also if the device is flat on a table, it cannot say for sure what its orientation is, but it knows for sure the orientation if the UI at all times. – Mecki Oct 28 '14 at 17:54
  • Just a correction, its not an "Unexpected value" when device is on a plain table. UIDeviceOrientation has a total of 7 values as declared in UIDevice.h, which includes UIDeviceOrientationUnknown , UIDeviceOrientationFaceUp, UIDeviceOrientationFaceDown too. You can log deviceOrientation value and see if it's equal to 5 or 6 for faceUp or faceDown. – cirronimbo Dec 14 '14 at 19:44
  • 8
    `self.interfaceOrientation` is deprecated in iOS 8. – KPM Mar 11 '15 at 13:34
  • also ios notification center and ios control center influences on `[[UIDevice currentDevice] orientation]` value. For example your app supports portrait mode only. In this case you'll get Notification Center panel and Control center panel coming from the left and right (not from the top and the bottom). In this case you will get Portrait values in `[[UIDevice currentDevice] orientation]` when your device is really in Landscape. – nickolay Jul 16 '15 at 15:11
26

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

beryllium
  • 29,669
  • 15
  • 106
  • 125
holodnyalex
  • 761
  • 5
  • 8
  • In addition, `[[UIDevice currentDevice] orientation]` "... always returns 0 unless orientation notifications have been enabled by calling beginGeneratingDeviceOrientationNotifications." (Docs.) – Olie Mar 13 '14 at 21:56
  • 9
    12 migraines and 5 ulcers later, I can confirm that `[[UIApplication sharedApplication] statusBarOrientation]` is a more reliable and simple way of detecting and managing orientation changes. – Clifton Labrum Oct 15 '14 at 00:06
3

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
1

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

Eduardo Irias
  • 1,043
  • 11
  • 12