Trait Collections

suggest change

In an iOS app, your user interface can take on one of a few different general shapes and sizes. These are defined using size classes, which are available through a view or view controller’s trait collection.

Apple defines two size classes: regular and compact. Each of these size classes are available on both axes of the device (horizontal and vertical). Your app may exist in any these four states throughout its lifetime. As a shorthand, developers often describe a size class combination by saying or writing the two size classes, with the horizontal axis first: “Compact/Regular” describes an interface that is horizontally compact but vertically regular.

In your app, use methods on the UITraitEnvironment protocol to check your current size class and respond to changes:

class MyViewController: UIViewController {
    override func viewDidAppear(_ animated: Bool) {
        print("Horizontal size class: \(traitCollection.horizontalSizeClass)")
        print("Vertical size class: \(traitCollection.verticalSizeClass)")

    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        print("Trait collection changed; size classes may be different.")

Both UIView and UIViewController conform to UITraitEnvironment, so you can look at your current trait collection and handle changes in subclasses of either.

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents