ui traits

Generated on 8/5/2024

1 search

The trait system in UIKit has seen significant improvements in iOS 18, making it easier to use and handle changes. Here are some key updates:

  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 setNeedsLayout or setNeedsDisplay.
    • This feature eliminates the need for manual trait change registration, enhancing performance by only creating necessary trait dependencies. For more details, you can refer to the What’s new in UIKit session.
  2. List Environment Trait:

    • UI collection view list sections and UITableViews now have the list environment trait set. This trait describes the style of the list that the view is in, ensuring that cells are styled appropriately in any given list.
    • 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. This simplifies the cell configuration code, as it no longer needs to know the style of the list. For more details, you can refer to the What’s new in UIKit session.
  3. Writing Tools Input Traits:

    • New tech input traits API allows customization of writing tools behavior to better suit your app. You can set the writing tool's behavior to limited for a panel experience or none to opt out completely for a text view. This is particularly useful for apps with custom text views other than UITextView. For more details, you can refer to the Get started with Writing Tools session.

Relevant Sessions

  1. What’s new in UIKit
  2. Get started with Writing Tools

These updates make it easier to manage and utilize traits in your iOS applications, enhancing both performance and ease of use.