what's new in iOS 18 related to custom trait collection types

Generated on 7/30/2024

1 search

Tailscan

This post is brought to you by Tailscan, the best devtool for Tailwind CSS.

In iOS 18, there are several new features related to custom trait collection types in UIKit:

  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 you access. If any of those traits change, UIKit automatically performs the associated invalidation for that method, such as calling setNeedsLayout or setNeedsDisplay. This eliminates the need for manual trait change registration. For example, if you read the horizontal size class trait in layoutSubviews, UIKit will automatically call setNeedsLayout when the horizontal size class changes (What’s new in UIKit).

  2. List Environment Trait: Collection and table view APIs have been updated to include a new list environment trait set. This trait describes the style of the list that the view is in, ensuring that cells are styled appropriately. UI list content configuration and UI background configuration now take advantage of this new trait, adjusting their properties to match the list environment from the configuration state's trait collection (What’s new in UIKit).

  3. New Constructors for UI Background Configuration: There are three new constructors for UI background configuration: listCell, listHeader, and listFooter. These configurations automatically update their appearance based on the list environment trait, simplifying the code needed to manage different list styles (What’s new in UIKit).

These enhancements make it easier to manage and respond to trait changes in your app, improving performance and reducing the need for manual updates.