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 .
Ehtd
source share