iOS Developer Library — Pre-Release

Developer

UIKit Framework Reference UITraitEnvironment Protocol Reference

Options
Deployment Target:

On This Page
Language:

UITraitEnvironment

Inheritance


Not Applicable

Import Statement


Swift

import UIKit

Objective-C

@import UIKit;

Availability


Available in iOS 8.0 and later.

The iOS interface environment, which includes such traits as horizontal and vertical size class, display scale, and user interface idiom, is available to apps through the UITraitEnvironment protocol. The following interface classes adopt this protocol: UIScreen, UIWindow, UIViewController, UIPresentationController, and UIView.

To access the trait environment of an object that adopts this protocol, use the traitCollection property. The protocol also provides an overridable method that the system calls when the interface environment changes. Implement this method as part of creating an adaptive iOS app.

For more about trait collections, see UITraitCollection Class Reference. For the WWDC 2014 presentation on creating adaptive interfaces in iOS, see Building Adaptive Apps with UIKit.

  • The trait collection for a view controller (an instance of the UIViewController class or one of its subclasses) or a view (an instance of the UIView class or one of its subclasses). (required) (read-only)

    Declaration

    Swift

    var traitCollection: UITraitCollection { get }

    Objective-C

    @property(nonatomic, readonly) UITraitCollection *traitCollection

    Discussion

    The UITraitEnvironment protocol is adopted by view controllers and views.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 8.0 and later.

  • Called when the iOS interface environment changes. (required)

    Declaration

    Swift

    func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?)

    Objective-C

    - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection

    Parameters

    previousTraitCollection

    The UITraitCollection object before the interface environment changed.

    Discussion

    The system calls this method when the iOS interface environment changes. Implement this method in view controllers and views, according to your app’s needs, to respond to such changes. For example, you might adjust the layout of the subviews of a view controller when an iPhone is rotated from portrait to landscape orientation. The default implementation of this method is empty.

    At the beginning of your implementation, call super to ensure that interface elements higher in the view hierarchy have an opportunity to adjust their layout first. Use code similar to this:

    • - (void) traitCollectionDidChange: (UITraitCollection *) previousTraitCollection {
    • [super traitCollectionDidChange: previousTraitCollection];
    • if ((self.traitCollection.verticalSizeClass != previousTraitCollection.verticalSizeClass)
    • || self.traitCollection.horizontalSizeClass != previousTraitCollection.horizontalSizeClass)) {
    • // your custom implementation here
    • }
    • }

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 8.0 and later.