7

InterfaceOrientation of UIViewController is deprecated and the Document suggest to use [[UIApplication sharedApplication] statusBarOrientation], but there is no sharedApplication in an extension of iOS 8.


As @Retro mentioned, in most circumstances, you can use self.traitCollection.verticalSizeClass or self.traitCollection.horizontalSizeClass in a UIViewController to get orientation information.

Albert Renshaw
  • 17,282
  • 18
  • 107
  • 195
Henry
  • 481
  • 4
  • 17

4 Answers4

11

A UITraitCollection object provides details about the characteristics of a UIViewController object, which manages a set of views that make up a portion of your app’s interface. These characteristics, or traits, define the size class, display scale, and device idiom of the view controller. When a view controller is created, a trait collection is automatically created for that view controller.

You can create and modify a view controller’s trait collection to customize your app. The following methods create a new trait collection containing only the passed parameter:

traitCollectionWithDisplayScale:

traitCollectionWithUserInterfaceIdiom:

traitCollectionWithHorizontalSizeClass:

traitCollectionWithVerticalSizeClass:
Necreaux
  • 9,451
  • 7
  • 26
  • 43
Retro
  • 3,985
  • 2
  • 17
  • 41
  • Unfortunately, the trait collection does not include orientation. Why Apple recommends this as a suitable replacement for [UIViewController interfaceOrientation] is beyond me. – cetcet May 06 '15 at 17:17
  • 2
    The idea is that you shouldn't think so much about 'orientation' anymore, but thing in terms of Size Classes. Your interfaces need to be adaptable, especially given the new iOS9 multi-tasking views, and the orientation of the device isn't going to an accurate judge of what your layout should look like at a given point. – Z S Jun 28 '15 at 19:31
  • This is not enough for cases like AVFoundation where the orientation makes a difference in parsing the data from the camera – jjxtra Aug 12 '16 at 13:59
3

Non-deprecated and will work on any device screen size (including future screen sizes Apple will be releasing this year).

-(void)viewDidLayoutSubviews {
    NSLog(@"%@", self.view.frame.size.width == fminf([[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height) ? @"Portrait" : @"Landscape");
}

Thanks @anneblue for helping shorten the code!

Albert Renshaw
  • 17,282
  • 18
  • 107
  • 195
  • 1
    As your solution compares the smaller dimension of the screen with the view's width, I simplified it to something this: `self.view.frame.size.width == fminf([[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height) ? @"Portrait" : @"Landscape"` – anneblue Nov 18 '15 at 13:36
  • @anneblue That's great! Thank-you. I haven't had time to test it yet, (I'm sure it works), if you already have please feel free to edit that into my answer! – Albert Renshaw Nov 18 '15 at 13:58
  • @anneblue I'll edit it in, sorry your edit was rejected by other members. – Albert Renshaw Nov 19 '15 at 17:22
  • I'm looking forward to a day when Apple'd follow the NEC lead and makes an iOS device with a 1:1 aspect ratio – Anton Tropashko Dec 30 '16 at 13:37
-1

You can convert screen bounds to keyboard view's coordinate system. After that you can check what is bigger width or height.

-3

Simple - just use:

[[UIApplication sharedApplication] statusBarOrientation]

cetcet
  • 2,147
  • 2
  • 15
  • 15
  • Well, this actually works in the app only. Once you want to read orientation in Safari Extension or similar, it's better to use [[UIDevice currentDevice] orientation] – http://stackoverflow.com/questions/7968451/different-ways-of-getting-current-interface-orientation – igraczech Dec 09 '15 at 09:56