How to answer
UIKit doesn't lay out immediately when something changes. You mark work as needed, and UIKit performs it once, at the end of the current run-loop turn, before the frame is rendered. The classic passes run top-down: update constraints (updateConstraints), layout (layoutSubviews, where the Auto Layout engine's frames are applied) and display (draw(_:)). There's also an update properties pass (updateProperties(), added in iOS 26), coalesced with the next layout pass and run before layoutSubviews, for pushing model state into view properties. Automatic trait tracking and observation tracking schedule these passes for you when traits or @Observable properties they read change.
Key points
- Request:
setNeedsUpdateConstraints(),setNeedsLayout(),setNeedsDisplay()andsetNeedsUpdateProperties(). Cheap, coalesced, deferred - Force now:
updateConstraintsIfNeeded(),layoutIfNeeded(),updatePropertiesIfNeeded(). Use when you need current frames immediately, e.g. inside an animation block - Override points:
updateProperties()for content and styling;layoutSubviews/viewWillLayoutSubviewsfor geometry;draw(_:)for custom drawing only - What triggers layout: bounds or size changes (rotation, window resize), adding or removing subviews, constraint changes, intrinsic size invalidation, trait changes
- Automatic tracking: traits read in layout methods and
@Observableproperties read inupdateProperties()/ layout methods re-invalidate the right pass automatically
If they dig deeper
Animating constraint changes: change constant, then call layoutIfNeeded() on the common superview inside the animation (with UIView.animate(springDuration:bounce:) for springs). Why updateConstraints is rarely worth overriding today (activate/deactivate constraints where the state changes). Why separating properties from layout helps: a text change shouldn't re-run geometry, and an @Observable model can drive updateProperties() without manual didSet → setNeedsLayout plumbing. Layout feedback loops: setting a frame or a constraint inside layoutSubviews that invalidates the parent's layout again (UIViewLayoutFeedbackLoopDebuggingThreshold helps find them).
Common pitfalls
Reading frames right after changing constraints without layoutIfNeeded(). Calling layoutIfNeeded() in loops or for every cell. Doing non-geometry work (setting text, colors, fetching) in layoutSubviews, which runs often during window resizing. Calling layoutSubviews() or updateProperties() directly (request them instead). Forgetting super in updateProperties().
How to use it
import UIKit
import Observation
@MainActor @Observable
final class BadgeModel { var count = 0; var isMuted = false }
final class BadgeView: UIView {
let model: BadgeModel
private let label = UILabel()
var widthConstraint: NSLayoutConstraint? // installed by the owner
init(model: BadgeModel) {
self.model = model
super.init(frame: .zero)
addSubview(label)
}
required init?(coder: NSCoder) { fatalError("init(coder:) is not supported") }
override func updateProperties() {
super.updateProperties()
label.text = "\(model.count)" // tracked; re-runs when count changes
alpha = model.isMuted ? 0.4 : 1 // no geometry here
}
override func layoutSubviews() {
super.layoutSubviews()
label.frame = bounds.insetBy(dx: 6, dy: 2) // geometry only
layer.cornerRadius = bounds.height / 2
}
func setExpanded(_ expanded: Bool, in container: UIView) {
widthConstraint?.constant = expanded ? 120 : 44
UIView.animate(springDuration: 0.4, bounce: 0.2) {
container.layoutIfNeeded() // apply the new constant inside the animation
}
}
}Best practices
- Request updates with
setNeeds…; force them with…IfNeeded()only when you need results now - Split responsibilities: properties in
updateProperties(), geometry inlayoutSubviews, drawing indraw(_:) - Keep layout methods cheap and idempotent; they run on every resize in iPadOS 26 windows and on iPhone Duo
- Animate constraint changes by updating constants and calling
layoutIfNeeded()on the superview in the animation block - Let trait and observation tracking invalidate for you instead of manual
didSet→setNeedsLayout
Related concepts
- Auto Layout, View lifecycle, Draw cycle (
setNeedsDisplay), Intrinsic content size, UIView animations, Observation in UIKit