What's the difference between the updateProperties method introduced and the viewWillLayoutSubviews introduced for Observation tracking in UIKit?

Asked on 2025-06-11

@wwdc25

2 searches

The updateProperties method and viewWillLayoutSubviews serve different purposes in UIKit's observation tracking and layout process.

updateProperties Method

  • Purpose: Introduced to support automatic trait and observation tracking in UIKit. It is a general-purpose update method that allows you to populate content, apply styling, or configure behaviors.
  • Timing: Runs just before layoutSubviews but is independent of it. This means you can invalidate properties without forcing a layout, allowing for finer-grained updates.
  • Functionality: Automatically tracks any observable you read, and you can manually trigger it by calling setNeedsUpdateProperties.
  • Use Case: Ideal for updating properties that depend on observable objects, such as updating a badge view when the underlying model changes, without triggering unnecessary layout passes.

viewWillLayoutSubviews

  • Purpose: Part of the traditional layout process in UIKit, used to make adjustments before the layout of subviews.
  • Timing: Called during the layout pass, after traits have been updated and before the actual layout logic is executed.
  • Functionality: Typically used to adjust the layout of subviews based on the current state of the view hierarchy.
  • Use Case: Suitable for making layout adjustments that are necessary due to changes in the view hierarchy or size.

In summary, updateProperties is designed to handle updates related to observable properties and is independent of the layout process, whereas viewWillLayoutSubviews is part of the layout process itself. This separation allows for more efficient updates and avoids unnecessary layout passes. For more details, you can refer to the session What’s new in UIKit (12:30).