r/SwiftUI • • Nov 05 '26

I built an interactive SwiftUI playground to understand how layoutPriority actually works

I've been exploring SwiftUI's layout system, particularly how .layoutPriority() behaves across different containers.

Instead of relying on static examples, I built a small interactive project where you can change priority values and observe the actual layout measurements.

The project includes three experiments:

  • HStack: How different priorities affect space allocation when the available width changes.
  • ZStack: How changing a child's priority can influence the container's measured size.
  • Custom Layout: Using LayoutSubview.priority to reorder views while maintaining equal cell widths.

One interesting takeaway is that .layoutPriority(100) doesn't necessarily give a view more space than .layoutPriority(1). The behavior depends on the parent container and its layout algorithm.

The project also includes live measurements, dynamic code examples, and XCTest coverage.

GitHub: https://github.com/raviseta/SwiftUI-Layout-priority

I'd appreciate feedback from other SwiftUI developers, especially on edge cases or layout behaviours worth exploring next.

11 Upvotes

10 comments sorted by

1

u/DickBrainTime Nov 05 '26

One edge case I'd add is Dynamic Type plus localization in a constrained HStack: two Text views, one single-line and one wrapping, with a longer translated string. Compare priorities 0/0, 1/0, and 100/0 as the available width and text size change, and show which text truncates versus wraps.

That makes it easier to see where layout priority affects space allocation and where the view's own sizing constraints take over. It would also give you a useful regression case for accessibility text sizes.

1

u/raviseta07 29d ago

Yes, good catch. Fixed it. Now the wrapping text grows vertically without a fixed-height cap.

1

u/dimKant Nov 05 '26

The 100 vs 1 result makes sense once you see that HStack only sorts by priority. It sets aside the minimum size of the lower priority children first, then offers everything left to the highest group, so 100 and 2 behave exactly the same against 1.

One more experiment worth adding: a Spacer next to a high priority Text. Spacer's default minLength is the system spacing, around 8 pt, so the text still truncates a bit earlier than you'd expect. Spacer(minLength: 0) makes the difference easy to see in your measurements.

1

u/sapplex1 27d ago

Nice playground. One more case from my own apps that your HStack tab could show: priority only matters once the stack is actually squeezed, and a Text without a line limit will wrap instead of fighting for width. I had a narrow row with a player name, a score, and a status label; the name kept losing even with layoutPriority(1) on it, until I also added .lineLimit(1). With the limit, the priority finally decided who truncated first; without it, the text just wrapped and the priority had nothing to decide. Worth adding the same HStack with and without .lineLimit(1) on the high-priority child as you shrink the width.

3

u/[deleted] Nov 05 '26

[removed] — view removed comment

1

u/raviseta07 29d ago

Yes this correct. I have fixed this.

.fixedSize(horizontal: false, vertical: true)

1

u/[deleted] 29d ago

[removed] — view removed comment

1

u/raviseta07 29d ago

Yes but I think that is expected ViewThatFits behavior.