iOS Developer Library — Pre-Release

Developer

UIKit Framework Reference UITraitEnvironment Protocol Reference

Options
Deployment Target:

On This Page
Language:

UITraitEnvironment

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. More...

Inheritance


Not Applicable

Import Statement


Swift

import UIKit

Objective-C

@import UIKit;

Availability


Available in iOS 8.0 and later.
  • 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

    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

    Swift

    import UIKit

    Availability

    Available in iOS 8.0 and later.