February 8, 2017
iOS Size Classes
Apple has introduced many devices like iPhone, iPad with different screen sizes and resolution. Also after iOS 8 apple has supported multitasking in iPad. So for the developers to develop a common or single UI for all the devices apple has introduced the concept of an adaptive layout by combining auto layout and size classes.
What is adaptive layout:
The adaptive layout is a method of building the apps based on the size and characteristics of the container instead of a targeting a particular device. We can create a single layout to work on all devices instead of writing a platform specific code. Adaptive layout includes size classes and trait collection etc.
Before size, a class has been introduced developers were using following approaches to developing the UI.
-Universal storyboards. Unique storyboard for iPhone and iPad.
-Monitoring the orientation change API for dynamic layout during device rotation.
-Using layout constraints to manage different size within single device class.
To avoid these apple has introduced size classes after iOS8.
About Size classes:
Size class identifies relative amount of display space for the height and width or vertically and horizontally. Size class categorizes each dimension into two terms.
– Compact: As the name indicates, if the container space is small to display the content then it is called as compact.
– Regular: If there is enough space in the container to display the content then this is called as Regular.
Using the size class, one can retrieve the general information about the device in its current orientation.
Size classes for different devices:
To support size classes few classes are added and modified they are:
– UITraitCollection: Each view or screen has its own trait collection. These include four properties. They are horizontalSizeClass , verticalSizeClass , userInterfaceIdiom
and displayScale. So each object defines its own trait collection using that can determine the size class, Device type, and device resolution. Classes that supports the UITraitEnvironment protocol(UIScreen, UIView, UIViewController etc) has its own trait collection.
– UIIImageAsset class is used to group the images based on different size class. For each size class, we can define the set of images.
– Classes that supports UIAppearance class can customize its objects like views based on its trait collection properties.
– From UIViewController one can retrieve the trait collection for its child view and also one can change the size class using the viewWillTransitionTosize:withTransitionCoordinatior method to layout the view.