swiftui · note · layout
layoutPriority in 10 Lines
Context
layoutPriority matters when SwiftUI has to decide which view should resist shrinking first.
That makes it useful for labels, titles, and compact rows where one element should stay readable.
The snippet
HStack {
Text("Very long title")
.layoutPriority(1)
Spacer()
Text("Detail")
.foregroundStyle(.secondary)
}
Why this works
The title tells SwiftUI it should get more room during layout negotiation. That is often enough to prevent awkward truncation without hard-coding widths.
Use it when:
- one element is more important than the others
- you want layout to stay adaptive
- text needs a better chance to remain visible
Avoid it when:
- the real problem is a broken container
- you are using it to patch over a bad hierarchy
- a simpler layout change would be clearer
Takeaway: Layout priority should describe importance, not compensate for bad structure.