UITraitCollection

Asked on 08/05/2024

1 search

The UITraitCollection in UIKit has received several updates in iOS 18, making it easier to use traits and handle changes. Here are some key points:

  1. Automatic Trait Tracking:

    • UIKit now supports automatic trait tracking inside common view and view controller update methods such as layoutSubviews and drawRect. When these methods are called, UIKit records which traits are accessed. If any of those traits change, UIKit automatically performs the associated invalidation for that method, such as calling setNeedsLayout or setNeedsDisplay.
    • This feature eliminates the need for manual trait change registration, improving performance by only creating necessary trait dependencies.

    For more details, you can refer to the session What’s new in UIKit (06:03).

  2. List Environment Trait:

    • UI collection view list sections and UITableViews now have the listEnvironment trait set. This trait describes the style of the list that the view is in, ensuring that cells are styled appropriately.
    • UIListContentConfiguration and UIBackgroundConfiguration now take advantage of this new trait, adjusting their properties to match the list environment from the configuration state’s trait collection.

    For more details, you can refer to the session What’s new in UIKit (07:54).

These enhancements streamline the use of traits in UIKit, making it more efficient and reducing the need for manual updates.