Traits and the trait environment
Get information about traits and the environment in which your app runs, and share data with your view hierarchy.
Overview
Traits represent independent pieces of data that UIKit automatically distributes through your app’s view and view controller hierarchies. The trait system provides a unified way to share configuration information, making it easier to build adaptive interfaces that respond to changes in device orientation, appearance modes, accessibility settings, and custom app states.
The trait system propagates values from the top of the hierarchy downward. When you modify a trait at any level using trait overrides, that change affects the modified object and all of its descendants. Setting a trait override on a window scene affects all view controllers and views within that scene. Setting a trait override on a specific view affects only that view and its subviews.
Use custom traits to propagate data that needs to flow through these parts of your view hierarchy:
Window scene (UIWindowScene)
Window (UIWindow)
Presentation controller (UIPresentationController)
View controller (UIViewController)
View (UIView)
Subview (UIView)
Custom traits are particularly useful for:
Configuration settings that affect entire sections of your interface
Appearance states that need to cascade through view hierarchies
Custom app themes or styling modes
Feature flags that enable or disable UI elements
Avoid using custom traits in cases where you can directly set properties on a view controller, view, or subview.
Topics
Essentials
Observing and managing traits
Automatic trait trackingUITraitCollectionUITraitEnvironmentUITraitChangeObservableUIMutableTraitsUIAdaptivePresentationControllerDelegateUIContentContainer