InterfaceOrientation deprecated in iOS 8
Asked Answered
B

3

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 ?

Bignonia answered 18/4, 2015 at 13:24 Comment(3)
The way to "fix it" is to use the new methods. Why don't you want to do that?Informality
"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.Fivefinger
How do I translate from UITraitCollection to UIInterfaceOrientation ? Example, which trait collection refers to Landscape left orientation ?Bignonia
P
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.

Peshitta answered 8/1, 2016 at 13:28 Comment(2)
But I have a problem with it, it is incapable of distinguishing between landscape left and landscape right. How do I get that?Bignonia
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]Peshitta
H
9

Should change

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

to

if (UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation])) {
  ...
}
Hylomorphism answered 22/10, 2015 at 4:43 Comment(1)
'statusBarOrientation' was deprecated in iOS 13.0 - #57966201Etheline
P
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.

Peshitta answered 8/1, 2016 at 13:28 Comment(2)
But I have a problem with it, it is incapable of distinguishing between landscape left and landscape right. How do I get that?Bignonia
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]Peshitta
D
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])) {
   ...
}
Dressler answered 30/4, 2018 at 11:38 Comment(1)
1. statusBarOrientation - deprecated, 2. Using device orientation is wrong. For example, device can be in portrait but interface - in landscape.Ungrateful

© 2022 - 2024 — McMap. All rights reserved.