UITraitCollection
A collection of data that represents the environment for an individual element in your app’s user interface.
Declaration
class UITraitCollectionMentioned in
Overview
The traitCollection property of the UITraitEnvironment protocol contains traits that describe the state of various elements of the iOS user interface, such as size class, display scale, and layout direction. Together, these traits compose the UIKit trait environment.
The following classes adopt UITraitEnvironment: UIScreen, UIWindow, UIWindowScene, UIViewController, UIPresentationController, and UIView. To create an adaptive interface, write code to adjust your app’s layout according to changes in these traits. You access specific trait values using the UITraitCollection horizontalSizeClass, verticalSizeClass, displayScale, userInterfaceIdiom, and other properties.
To make your view controllers and views responsive to changes in the iOS interface environment, use automatic trait tracking in supported UIViewController and UIView methods, or register to track specific trait changes with UITraitChangeObservable methods. For more information, see Adapting your app when traits change.
To customize view controller animations in response to interface environment changes, override the willTransition(to:with:) method of the UIContentContainer protocol.
For more information about the horizontal (width) and vertical (height) size classes your app can encounter when running full-screen on various devices, see Human Interface Guidelines > Layout.
You can create standalone trait collections to assist in matching against specific environments. The UITraitCollection class includes four specialized constructors, as well as a constructor that enables you to combine an array of trait collections, init(traitsFrom:).
One important use of standalone trait collections is to enable conditional use of images based on the current iOS interface environment. You can associate a trait collection with a UIImage instance by way of a UIImageAsset instance, as described in the overview section of UIImageAsset. For information on configuring asset catalogs graphically from within the Xcode IDE, see Managing assets with asset catalogs.
You can employ a standalone trait collection to enable a two-column split view in landscape orientation on iPhone. See the setOverrideTraitCollection(_:forChild:) method of the UIViewController class.
You can also use a standalone trait collection to customize view appearance with the appearance(for:) protocol method, as described in UIAppearance.
For information on creating custom traits, see Providing data to the view hierarchy with custom traits.
Topics
Getting the current traits
Getting related traits
Modifying traits
Getting trait changes
Comparing trait collections
Performing actions with the current traits
Retrieving size class traits
Retrieving display-related traits
Retrieving interface-related traits
userInterfaceStyleUIUserInterfaceStyleuserInterfaceIdiomUIUserInterfaceIdiomuserInterfaceLevelUIUserInterfaceLevellayoutDirectionUITraitEnvironmentLayoutDirectionresolvesNaturalAlignmentWithBaseWritingDirectionaccessibilityContrastUIAccessibilityContrastlegibilityWeightUILegibilityWeightactiveAppearanceUIUserInterfaceActiveAppearancetoolbarItemPresentationSizeUINSToolbarItemPresentationSizehdrHeadroomUsageLimitUIHDRHeadroomUsageLimit
Retrieving the force touch capability traits
Retrieving content size category information
Retrieving layout environment traits
listEnvironmentUIListEnvironmentsplitViewControllerLayoutEnvironmentUISplitViewController.LayoutEnvironmenttabAccessoryEnvironmentUITabAccessory.Environment
Retrieving scene capture state
Retrieving dynamic range traits
Retrieving typesetting language traits
Getting an image configuration object
Creating a trait collection
init()init(userInterfaceIdiom:)init(horizontalSizeClass:)init(verticalSizeClass:)init(userInterfaceStyle:)init(accessibilityContrast:)init(userInterfaceLevel:)init(legibilityWeight:)init(forceTouchCapability:)init(displayScale:)init(displayGamut:)init(layoutDirection:)init(preferredContentSizeCategory:)init(activeAppearance:)init(toolbarItemPresentationSize:)init(hdrHeadroomUsageLimit:)init(imageDynamicRange:)init(listEnvironment:)init(resolvesNaturalAlignmentWithBaseWritingDirection:)init(sceneCaptureState:)init(tabAccessoryEnvironment:)init(typesettingLanguage:)init(coder:)init(traitsFrom:)
Initializers
init(HDRHeadroomUsageLimit:)init(_:value:)init(_:value:)init(_:value:)init(_:value:)init(_:value:)init(_:value:)init(_:value:)init(_:value:)init(_:value:)init(_:value:)init(_:value:)init(_:value:)init(_:value:)init(_:value:)init(_:value:)init(_:value:)init(traitsFromCollections:)
Instance Methods
replacing(_:value:)replacing(_:value:)replacing(_:value:)replacing(_:value:)replacing(_:value:)replacing(_:value:)replacing(_:value:)replacing(_:value:)replacing(_:value:)replacing(_:value:)replacing(_:value:)replacing(_:value:)replacing(_:value:)replacing(_:value:)replacing(_:value:)replacing(_:value:)