WWDC 2023: Unleash the UIKit trait system

Find hereafter a detailed summary of a video that belongs to a taxonomy of some WWDC footages.

The original video is available on the Apple official website (session 10057).

"Discover powerful enhancements to the trait system in UIKit. Learn how you can define custom traits to add your own data to UITraitCollection, modify the data propagated to view controllers and views with trait override APIs, and adopt APIs to improve flexibility and performance. We'll also show you how to bridge UIKit traits with SwiftUI environment keys to seamlessly access data from both UIKit and SwiftUI components in your app."

Hereafter, the underlined elements lead directly to the playback of the WWDC video at the appropriate moment.

Traits #

Traits are built-in pieces of data that are automatically propagated through the app thanks to the trait collections.

iOS 17 has brought some new APIs for streamlining the work with trait collections.

🎬

This initializer that contains a closure is concomitantly highlighted with the new UIMutableTraits protocol.



Trait environment #

The trait environments (window scenes, windows,...) have their own trait collection and are connected through the objects hierarchy for conveying their content.

The trait collection inheritance has been modified in iOS 17 in order to have a direct link between a view controller and its view’s superview instead of inheriting the trait collection from the parent view controller.

Trait update #

According to the brand new flow of traits in iOS 17, the view controller trait update changes in some important aspects...

... whilst affecting the view trait update as well.



New features & capabilities #

Custom traits #

Uses for custom traits are disparate and should be exploited in added-value cases.

🎬

Defining a type representing a trait in a trait collection is basic and should come with some specific extensions so as to access the trait everywhere.

🎬

The second example is based on the components of a color object that depend on the active traits.

The block is used for dynamically generating the color data but it's important to notice that traits dealing with color appearance should be used parsimoniously due to their expensiveness for the system.



Good practices are provided to get the best result when trait customization is intended...

... not to mention the Objective-C coding that takes into account this alternative.

Override traits #

iOS 17 has introduced a new property for modifying data within the trait hierarchy.

Managing the overrides is made easier through dedicated methods.

Best practices are pointed out for maximizing the performance when overriding traits.


Handle changes #

Managing changes for traits may be embedded in a threefold goal:

🎬

This registration can be done for many traits on different views as well.

🎬

The registration for many traits is also possible including the previous trait collection information.



iOS 17 provides new semantic system traits to be used inside the registrations.

These registrations are automatically cleaned up but can be manually unregistered if need be.


SwiftUI bridging #

The compatibility between UIKit and SwiftUI relies on the UITraitBridgedEnvironmentKey protocol that connects the environment key in SwiftUI and the trait in UIKit.