9

I am not using Size classes in my project and continue to use old methods for view controller orientation. I am getting deprecation warnings, such as when I use code below :

    if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) {
      ...
    }

I have searched a lot but could not find the right way to fix it. Any suggestions ?

Deepak Sharma
  • 5,577
  • 7
  • 55
  • 131
  • The way to "fix it" is to use the new methods. Why don't you want to do that? – Aaron Brager Apr 18 '15 at 13:34
  • "I am not using Size classes in my project" Well, there's your problem. Orientation-related info is communicated to you through the `traitCollection` as size classes. Obviously if you refuse to look at it, you won't see it. – matt Apr 18 '15 at 14:23
  • How do I translate from UITraitCollection to UIInterfaceOrientation ? Example, which trait collection refers to Landscape left orientation ? – Deepak Sharma Apr 19 '15 at 17:55

3 Answers3

9

Should change

if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) {
  ...
}

to

if (UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation])) {
  ...
}
Tharoth
  • 383
  • 5
  • 15
  • 'statusBarOrientation' was deprecated in iOS 13.0 - https://stackoverflow.com/questions/57965701/statusbarorientation-was-deprecated-in-ios-13-0-when-attempting-to-get-app-ori – Vigneshwaran Murugesan May 24 '22 at 04:48
7

Since iOS8, Apple recommends to use TraitCollections (Size Classes) instead of interfaceOrientation.

Moreover, since iOS 9 and the new iPad feature "Multitasking", there are some cases where the device orientation doesn't fit with the window proportions ! (This leads to brake your application UI)

However, sometimes TraitCollections doesn't fill all your design needs. For those cases, Apple recommends to compare view's bounds :

if view.bounds.size.width > view.bounds.size.height {
    // ...
}

I was quite surprised, but you can check on the WWDC 2015 video Getting Started with Multitasking on iPad in iOS 9 at 21'15.

vmeyer
  • 1,998
  • 21
  • 23
  • But I have a problem with it, it is incapable of distinguishing between landscape left and landscape right. How do I get that? – Deepak Sharma Apr 06 '17 at 12:09
  • You're right @DeepakSharma, however size classes allow you neither to distinguish between landscape left and landscape right. Why do you need this information ? If you want the device orientation and not the screen orientation, you can use `[[UIApplication sharedApplication] statusBarOrientation]` – vmeyer Apr 07 '17 at 13:15
0

Some of you may need to use UIDevice Orientation

if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) {
   ...
}

instead of Status Bar Orientation

if (UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation])) {
   ...
}
S. George
  • 36
  • 3
  • 1. statusBarOrientation - deprecated, 2. Using device orientation is wrong. For example, device can be in portrait but interface - in landscape. – Cynichniy Bandera Apr 02 '20 at 08:41