What is the easiest way to get a UIViewController size class?

In the JFC lecture on WWDC 2014, Apple Engineer introduced size classes , and he said that we can think that the iPhone UI size as a compact size class and the iPad regular size class as a regular size class . But size classes are not limited to a specific device. Then they are much more general. If the viewcontroller is similar to the iPhone - its aspect ration is similar to it - it will have a compact size class.

Is there a way to see at a specific time which size class the viewcontroller used ? I found contradictions between the contents of the simulator and the preview of the Builder interface, and I would like to go deeper and understand why this is happening.

+7
ios size-classes
source share
1 answer

I'm currently considering how to use size classes programmatically, here are some finds that might be useful to you:

The easiest way to find out is to simply check the view controllers traitCollection

 po self.traitCollection 

Or listening to transitions:

First use the UIContentContainer protocol

 @interface ViewController : UIViewController<UIContentContainer> 

Then we willTransitionToTraitCollection:

 - (void)willTransitionToTraitCollection:(UITraitCollection *)newCollection withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator { } 

The newCollection element contains information that can be used for your purpose, here is the information returned when you turn iPhone 6 into an album:

 <UITraitCollection: 0x7f9ad152f320; _UITraitNameUserInterfaceIdiom = Phone, _UITraitNameDisplayScale = 2.000000, _UITraitNameHorizontalSizeClass = Compact, _UITraitNameVerticalSizeClass = Compact, _UITraitNameTouchLevel = 0, _UITraitNameInteractionModel = 1> 

And for the portrait:

 <UITraitCollection: 0x7f9ad142d770; _UITraitNameUserInterfaceIdiom = Phone, _UITraitNameDisplayScale = 2.000000, _UITraitNameHorizontalSizeClass = Compact, _UITraitNameVerticalSizeClass = Regular, _UITraitNameTouchLevel = 0, _UITraitNameInteractionModel = 1> 

From there you can see that it uses the Compact Size class for horizontal and vertical orientation in the landscape , but uses the standard size class for vertical when in portrait .

+12
source share

All Articles