What's the difference between the updateProperties method introduced and the viewWillLayoutSubviews introduced for Observation tracking in UIKit? Which should be used in what context?

Asked on 06/11/2025

1 search

The updateProperties method and viewWillLayoutSubviews serve different purposes in UIKit, particularly with the introduction of automatic observation tracking.

updateProperties Method

  • Purpose: It is a new method introduced to support automatic trait and observation tracking. It allows you to update properties without forcing a layout, providing finer-grained updates.
  • Usage: Use updateProperties to populate content, apply styling, or configure behaviors. It automatically tracks any observable you read, and you can manually trigger it by calling setNeedsUpdateProperties.
  • Timing: It runs just before layoutSubviews but is independent of it, allowing you to invalidate properties without triggering a layout pass.
  • Example: If you have an observable model object, changes to this model will trigger updateProperties, allowing you to update the view accordingly without unnecessary layout passes.

viewWillLayoutSubviews

  • Purpose: This method is part of the traditional layout cycle in UIKit, used to make changes just before the layout of subviews.
  • Usage: Use viewWillLayoutSubviews when you need to adjust the layout of subviews in response to changes in the view's bounds or other layout-related changes.
  • Timing: It is called during the layout pass, after updateProperties has been executed.

When to Use Each

  • Use updateProperties when you need to update properties based on observable changes without affecting the layout. It is ideal for updating content or styles that do not require a layout pass.
  • Use viewWillLayoutSubviews when you need to adjust the layout of subviews in response to changes in the view's size or other layout-related events.

For more details, you can refer to the session What’s new in UIKit (12:30) where these methods are discussed.