-1

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.

  • Why do you need this? What are you trying to achieve? Please update your question (no comments) with more details. – rmaddy Oct 03 '18 at 18:26
  • 1
    @maddy Done. Why would it matter though? –  Oct 03 '18 at 18:29
  • In most cases an app never needs to deal with the orientation. So you are either doing something unique or you are doing something incorrectly. And your recent edit makes your question even less clear. You need to show relevant code. You need to explain what you are trying to do and what problem it is having. – rmaddy Oct 03 '18 at 18:33
  • 1
    @maddy What? I just need the UIInterfaceOrientation to make a layout. I am laying out something via SpriteKit. All I need to do is get the correct output from UIInterfaceOrientation. –  Oct 03 '18 at 18:35
  • Great. Put that in your question along with relevant code. Once people know what you actually need, people can offer proper solutions. – rmaddy Oct 03 '18 at 18:37
  • @maddy I'm sorry but exactly what code would be relevant? I just need to have a short snippet of code to detect the orientation, no matter what my context is. It just needs to be a standalone piece of code which I say to run and it gives me one of the four possibilities. –  Oct 03 '18 at 18:41
  • @user10176969 rmaddy is just trying to be helpful. You should really try to rethink the layout design so that it doesn't depend on the orientation of the device if at all possible. – Upholder Of Truth Oct 03 '18 at 20:02

1 Answers1

0

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

Scott Thompson
  • 22,629
  • 4
  • 32
  • 34
  • 1
    How do I use the switch command to get each case though? I've tried `switch UIInterfaceOrientation {` and it isn't working. –  Oct 04 '18 at 08:11
  • As other commenters have said - You should not be using UIInterfaceOrientation constants to define the behavior of your view when the device rotates. As the documentation states you should be drawing your view the same every time and rely on the view controller to rotate your view. If you want to know when that happens, your view controller will get a notification that the trait collection will change. Please read the referenced documentation. – Scott Thompson Oct 05 '18 at 00:43