Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 219
János Avatar asked Jul 29 '14 20:07

János


People also ask

What is Autolayout What is size classes and how can we use it?

In easy way we can say Autolayout is used for displaying same layout comatible on different different iPhone/iPad screen sizes (ex. Keep button in center for all screen sizes) while through Autoresizing classes we can set a different layout for a particular screen size.

What are size classes?

What are Size Classes? In iOS, Size Classes are groups of screen sizes that are applied to the width and height of the device screen. The two Size Classes that exist currently are Compact and Regular. The Compact Size Class refers to a constrained space.

What is a UIViewController subclass?

The UIViewController class defines the shared behavior that's common to all view controllers. You rarely create instances of the UIViewController class directly. Instead, you subclass UIViewController and add the methods and properties needed to manage the view controller's view hierarchy.

What is size classes and how can we use it?

Size Classes are the iOS method of creating adaptable layouts that look great on all sizes and orientations of iPhone and iPad. For example, you might want to say that your UI looks mostly the same in portrait and landscape, but on landscape some extra information is visible.


1 Answers

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.

like image 76
EhTd Avatar answered Nov 04 '22 02:11

EhTd