With Adaptive Layout you are able to conditionally install Auto Layout constraints or views. Since iOS8 it is easier than ever to write universal apps for iPhone and iPad devices. Overriding the size class can be an incredibly useful tool in your Adaptive Layout tool belt.
Trait collection
Size-classes are part of a trait collection. A trait collection is a set of characteristics that describe the current environment. These include the horizontal and vertical size-classes, the screen scale, and the device idiom. The UITraitCollection class wraps these in a nice, convenient object.
Trait collections can be obtained from any object conforming to the UITraitEnvironment protocol. This includes UIViewController and UIView, which means you can get hold of the current trait environment at any time.
Devices have an intrinsic trait collection for a given orientation, and this is passed down through the view controller and view hierarchies. However, it is possible to override the trait collection of a child view controller using setOverrideTraitCollection(_:, forChildViewController:). You’ll use this to override the vertical size class.
Overriding size classes
To support Adaptive Layout, UIViewController – viewWillTransitionToSize(_:,withTransitionCoordinator) is called when the bounds of a view change during rotation. You can exploit this method, using it to hook in to the transition animation.
This is a utility method that takes a CGSize as an argument. If the height of this size is less than 1000 then it creates an instance of UITraitCollection with a Compact vertical size class.
Note: Providing nil to the first parameter will reset any previous overrides and the trait collection will return to its default behavior, that is, deferring to the trait collection of the container’s parent.
You need to call this method only from two places. First in viewDidLoad() and second in viewWillTransitionToSize(size: CGSize).
Using the size you can now make your layout, images or fonts size dependent.
