Replace device orientation with interface orientation

Viewed 1909

I have been using this for orientation changes

  if (UIDevice.current.orientation == UIDeviceOrientation.portrait)

How do I replace this by UIInterfaceOrientation.portrait in the above syntax. This is because UIDeviceOreientation has faceup and facedown, which are of no use to me.

2 Answers

You can replace this by the below line

if (UIApplication.shared.statusBarOrientation == UIInterfaceOrientation.portrait)

Trungduc's answer will work but as Apple states in their documentation:

Starting in iOS 8, you should employ the UITraitCollection and UITraitEnvironment APIs, and size class properties as used in those APIs, instead of using UIInterfaceOrientation constants or otherwise writing your app in terms of interface orientation.

Edit: if you don't want to get into the flexibility UITraitCollection gives you you can keep your method but simplify it:

if (UIDevice.current.orientation.isPortrait) {
}
Related