user10176969
user10176969

Reputation:

How do I get an output for UIInterfaceOrientation?

I would like to be able to get the orientation of my iOS application's view using UIInterfaceOrientation.

This is different to UIDevice.current.orientation as that returns how the device should rotate rather than its actual rotation and has the possibility of returning unknown.

I have noticed UIInterfaceOrientation.portrait, UIInterfaceOrientation.portraitUpsideDown, UIInterfaceOrientation.landscapeLeft and UIInterfaceOrientation.landscapeRight but I am not sure how to detect these.

I will need this so that my User Interface can update correctly, when UIDevice.current.orientation returns an unknown or an incorrect orientation it ruins the UI.

Upvotes: -1

Views: 826

Answers (1)

Scott Thompson
Scott Thompson

Reputation: 23701

Please read the documentation here:

https://developer.apple.com/documentation/uikit/uiinterfaceorientation

particularly the first paragraph which reads:

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.

Generally speaking, your code should not depend on the device's current interface orientation. Instead it should rely on the traits of the current view controller to handle orientation changes. For more information see:

https://developer.apple.com/library/archive/featuredarticles/ViewControllerPGforiPhoneOS/TheAdaptiveModel.html

Upvotes: 0

Related Questions