3

Since iOS8 methods to detect the orientation of a UIViewController are deprecated.
For instance -interfaceOrientation is deprecated, to detect the current view controller orientation it seems that we need to ask this information to the -traitCollection property on view controllers.
I'm bit confused iPad has his traits set to regular in both orientation.
What is the best approach to detect current orientation?

Andrea
  • 26,120
  • 10
  • 85
  • 131

4 Answers4

2

Using -orientation property of UIDevice is not correct (even if it could work in most of cases) and could lead to some bugs, for instance UIDeviceOrientation consider also the orientation of the device if it is face up or down, there is no pair in UIInterfaceOrientation enum for those values.
Furthermore, if you lock your app in some particular orientation, UIDevice will give you the device orientation without taking that into account.
On the other side iOS8 has deprecated the interfaceOrientation property on UIViewController class.
There are 2 options available to detect the interface orientation:

  • Use the status bar orientation
  • Use size classes, on iPhone if they are not overridden they could give you a way to understand the current interface orientation

What is still missing is a way to understand the direction of a change of interface orientation, that is very important during animations.
In the session of WWDC 2014 "View controller advancement in iOS8" the speaker provides a solution to that problem too, using the method that replaces -will/DidRotateToInterfaceOrientation.

Here the proposed solution partially implemented:

-(void) viewWillTransitionToSize:(CGSize)s withTransitionCoordinator:(UIVCTC)t {
    orientation = [self orientationFromTransform: [t targetTransform]]; 
    oldOrientation = [[UIApplication sharedApplication] statusBarOrientation]; 
    [self myWillRotateToInterfaceOrientation:orientation duration: duration]; 
    [t animateAlongsideTransition:^(id <UIVCTCContext>) {
         [self myWillAnimateRotationToInterfaceOrientation:orientation
                                                  duration:duration];
      }
      completion: ^(id <UIVCTCContext>) {
         [self myDidAnimateFromInterfaceOrientation:oldOrientation];
      }];
}
Andrea
  • 26,120
  • 10
  • 85
  • 131
1

Look up the UIDeviceOrientation part of UIDevice and its orientation property. The docs are here. This is how I do my orientation checks. You can also see if the screen width is larger than the screen height since sometimes, especially on first launch of the app, it will be UIDeviceOrientationUnknown.

Doug Watkins
  • 1,387
  • 12
  • 19
  • I don't think that UIDeviceOrientation should be used as a replacement for UIInterfaceOrientation. The fact is that if you put your device up side, you get this orientation and not the interface orientation. UIDeviceOrientation works well when you need a 3d representation of your device, not 2d interface. – Andrea Feb 22 '15 at 16:48
  • I must have misunderstood what you are trying to accomplish then. – Doug Watkins Feb 22 '15 at 17:42
1

Best practice is to use size classes rather than device orientation.

The iPad has regular size in both orientations because, regardless of orientation, the iPad has plenty of room for content both vertically and horizontally.

You should consider why you wish to treat the iPad display differently in portrait vs. landscape orientation, and explain more clearly here why size classes don't fulfill your need.

  • 3
    Sometimes it's just because the customer want it. Furthermore in a wwdc2014 session they also explain how to achieve that – Andrea Jun 15 '15 at 19:18
1

Since iOS 9 and the new Multitasking feature, Apple recommands to use TraitCollection OR compare view bounds to fit with your design needs.

Take a look at my answer on a similar question : apple recommends you to compare view bounds

Community
  • 1
  • 1
vmeyer
  • 1,998
  • 21
  • 23