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.

Coder221
  • 1,403
  • 2
  • 19
  • 35

2 Answers2

7

You can replace this by the below line

if (UIApplication.shared.statusBarOrientation == UIInterfaceOrientation.portrait)
trungduc
  • 11,926
  • 4
  • 31
  • 55
  • 1
    I have an answer that says it is deprecated, so should I leave my syntax as it is without any change? – Coder221 Nov 05 '17 at 07:45
  • @Coder221 let try and you will know should you use it or not ;) – trungduc Nov 05 '17 at 07:54
  • @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. – trungduc Nov 05 '17 at 07:58
  • yup that was my question, maybe I should ask another question regarding it. – Coder221 Nov 05 '17 at 08:00
  • @Coder221 Let's investigate a little before making a new question. I think you can figure out the answer by yourself ;) – trungduc Nov 05 '17 at 08:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/158249/discussion-between-coder221-and-trungduc). – Coder221 Nov 05 '17 at 08:05
  • you mind looking at this question https://stackoverflow.com/questions/48303609/call-viewwilltransition-in-didselectitemat-indexpath – Coder221 Jan 17 '18 at 14:59
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) {
}
Terje
  • 980
  • 9
  • 15
  • Then what would be the syntax, which is not deprecated? – Coder221 Nov 05 '17 at 07:42
  • @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 https://stackoverflow.com/questions/47119076/corner-radius-on-a-uiview-not-working-as-expected/47119191#47119191 – trungduc Nov 05 '17 at 07:52
  • Can you please explain flexibility of UITraitCollection in terms of my syntax – Coder221 Nov 05 '17 at 07:57
  • 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. – Terje Nov 05 '17 at 08:03
  • 1
    Appreciate your explanation, will look into it. – Coder221 Nov 05 '17 at 08:22