3

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 2

7

You can replace this by the below line

if (UIApplication.shared.statusBarOrientation == UIInterfaceOrientation.portrait) 
Sign up to request clarification or add additional context in comments.

7 Comments

I have an answer that says it is deprecated, so should I leave my syntax as it is without any change?
@Coder221 let try and you will know should you use it or not ;)
@Coder221 it's up on you. But let's look at your question, you ask for How do I replace this by UIInterfaceOrientation.portrait in the above syntax ;) and i'm sure that there won't have any problem with your application if you use my code.
yup that was my question, maybe I should ask another question regarding it.
@Coder221 Let's investigate a little before making a new question. I think you can figure out the answer by yourself ;)
|
2

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) { } 

5 Comments

Then what would be the syntax, which is not deprecated?
@Terje you are very good guy and please don't copy another answer ;). Good luck. Hope your answer can be marked as right answer in this question stackoverflow.com/questions/47119076/…
Can you please explain flexibility of UITraitCollection in terms of my syntax
Just in terms of your syntax you have no need for UITraitCollection. Without knowing more about your app I can't really comment but there are some good blog intros to UITraitCollection out there if you Google. I think the point Apple is trying to make is to not necessarily think just in terms of portrait and landscape now that we are starting to have many different screen sizes and devices but to rather adapt your interface on a more fine grained level.
Appreciate your explanation, will look into it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.