What is the easiest to get the size class of a UIViewController?
Asked Answered
O

1

8

In the WWDC 2014 Platform State of the Union lecture Apple Engineer introduced size classes and he said we can think iPhone size UI as a compact size class and iPad size class as a regular size class. But size classes are not bounded to a particular device. They are a lot more general then that. If a viewcontroller look like an iPhone - its aspect ration is similar to it -, it will have a compact size class.

Is it a way to see at a certain time which size class was used by viewcontroller? I found contradictions between simulator content and Interface Builder preview and I would like to dig deeper into and see why it happens.

Octet answered 29/7, 2014 at 20:41 Comment(4)
... you should probably watch the WWDC 2014 videos on the apple dev website.... and learn how to use the classes. They should go over all of this and proper 'expected' behavior versus beta behavior.Blowpipe
Session 216 - Building Adaptive Apps with UIKitBigley
and here's the link (developer.apple.com/videos/play/wwdc2014/102)Utopian
... between 1:04:00 and 1:09:00Utopian
G
11

I am currently looking on how to use size classes programatically here are some finding that can be useful for you:

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

po self.traitCollection

Or listen to transitions:

First use the UIContentContainer protocol

@interface ViewController : UIViewController<UIContentContainer>

Then implement willTransitionToTraitCollection:

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

}

The newCollection element has information that can be used for your purpose, here is the info returned when rotating an iPhone 6 to landscape:

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

And to portrait:

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

From there you can see it uses Compact Size Class for both horizontal and vertical in landscape, but uses Regular Size Class for Vertical when in portrait.

Governess answered 30/9, 2014 at 17:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.