Automatic trait tracking
Reduce the need to manually register for trait changes when you use traits within a method or closure that supports automatic trait tracking.
Overview
Automatic trait tracking is a UIKit feature that eliminates the need to manually register for trait changes when you use traits in a supported method or closure. This feature reduces the amount of code you need to write and maintain, improves performance, and encourages the best practice of using traits within the scope of the supported APIs. For more information, see Adapting your app when traits change.
Some properties aren’t appropriate to change during layoutSubviews(), for example, properties where setting the value has a side-effect of invalidating the view’s layout. Update these properties in a view’s updateProperties() method, or a view controller’s updateProperties() method. These methods support automatic trait tracking, and automatic observation tracking on objects that use the Observable() macro. Notify an object of other updates to its properties by calling setNeedsUpdateProperties() on your view, or setNeedsUpdateProperties() on your view controller. Force an object to immediately update its properties by calling updatePropertiesIfNeeded() on your view, or updatePropertiesIfNeeded() on your view controller. For more information on automatically observing property updates, see Updating views automatically with observation tracking.
A complete list of APIs that support automatic trait tracking appears below.
Topics
Views
updateProperties()setNeedsUpdateProperties()updatePropertiesIfNeeded()layoutSubviews()updateConstraints()draw(_:)UIView.Invalidations.Properties
View controllers
updateProperties()setNeedsUpdateProperties()updatePropertiesIfNeeded()viewWillLayoutSubviews()viewDidLayoutSubviews()updateViewConstraints()updateContentUnavailableConfiguration(using:)