Automatic Trait Tracking
In iOS 18, UIKit introduces automatic trait tracking, which eliminates the need to manually register for trait change notifications in layout update methods. This feature is supported in various methods of views and view controllers, including UIView's layoutSubviews, updatesConstraints, and draw, as well as UIViewController's viewWillLayoutSubviews and updateViewConstraints. When UIKit calls one of these methods, it notes which traits are accessed and automatically invalidates the view when those traits change. This is done using methods like setNeedsLayout, setNeedsUpdateConstraints, setNeedsDisplay, or setNeedsUpdateConfiguration. For example, in a UIView subclass that overrides draw, accessing the preferredContentSizeCategory trait will trigger an automatic call to setNeedsDisplay when the trait changes. Prior to iOS 18, developers had to manually register for trait changes and call setNeedsDisplay to trigger a new call to draw. However, with automatic trait tracking, this process is now handled by UIKit. This feature simplifies the process of responding to trait changes and reduces the amount of code needed. Automatic trait tracking is a significant improvement in iOS 18, making it easier for developers to create adaptive and responsive user interfaces.