In SwiftUI, when a product title truncates or wraps unexpectedly beside a price in an HStack, how do fixedSize and layoutPriority change the result?
answer
- who gets squeezed first
- default priority is zero
- the ideal size of a string
- fixing one axis only
basics
~20 sIn a SwiftUI HStack, layoutPriority(_:) decides which sibling keeps its space: a higher-priority view gets everything except its siblings' minimum. fixedSize() pins a view at its ideal size, so it never truncates but can overflow its parent.
solid answer
~50 sWhen an `HStack` is offered less width than its children ideally need, `Text` views adapt by wrapping, tightening or truncating, and with the default `layoutPriority` of 0 the stack shares the shortfall between them. `layoutPriority(_:)` changes that: Apple's documentation says a parent offers its highest-priority children all the space it was offered minus the minimum required by the lower-priority ones, so `.layoutPriority(1)` on the price keeps it whole and the title absorbs the squeeze. `fixedSize()` is stronger and blunter: it fixes the view at its ideal size, which for a `Text` is the width of the entire string, so it cannot truncate but may push past the parent's bounds. The axis variant `fixedSize(horizontal: false, vertical: true)` keeps the proposed width and fixes the height, the usual fix for a multi-line title that is cut off vertically.
code
swift · 17 linesimport SwiftUI
struct ProductPriceRow: View {
let title: String
let price: Decimal
var body: some View {
HStack {
Text(title)
.font(.headline)
Spacer(minLength: 8)
Text(price, format: .currency(code: "EUR"))
.font(.headline.monospacedDigit())
.layoutPriority(1)
}
}
}go deeper
Recall the two tools: layoutPriority(_:) decides which sibling is squeezed last, and fixedSize() keeps a view at its natural size.
Explain the mechanics: default priority 0 shares space evenly, a higher priority gets all the space minus the others' minimum, and fixedSize can overflow the parent.
Pick the least forceful fix and test it at the largest Dynamic Type size and in a long-string locale, where fixedSize most often turns truncation into overflow.
Agree a rule for shared row components: which element in a row always wins space, expressed as layout priority rather than fixed widths, so localisation never needs per-language patches.
## The setup A product row shows a name and a price side by side: ```swift HStack { Text("Organic Cotton Crew-Neck Sweater") Spacer() Text(price, format: .currency(code: "EUR")) } ``` On a narrow phone, or at a large Dynamic Type size, the row is offered less width than both strings need. Two failure modes appear: the price truncates to an ellipsis, or the title wraps onto lines nobody designed for. ## Why it happens A `Text` answers a proposal smaller than its ideal width by wrapping, tightening, scaling or truncating. Its **ideal width** is, as Apple puts it, the space needed to represent the entire string. When the stack measures its children, each text can shrink, so the stack has to decide who gives way. Views typically have a default layout priority of `0`, which per Apple's documentation causes space to be apportioned evenly among siblings. Neither the price nor the title is special, so both lose. ## layoutPriority: choose who keeps their space `layoutPriority(_:)` takes a `Double`. Apple describes its effect in two sentences worth quoting accurately: - Raising a view's priority encourages it to **shrink later** when the group is shrunk and **stretch sooner** when the group is stretched. - A parent layout offers the children with the highest priority **all the space offered to the parent minus the minimum space required for all its lower-priority children**. So `.layoutPriority(1)` on the price means the stack satisfies the price first and the title absorbs the shortage, wrapping or truncating instead. Only the relative order matters; `1` is enough to beat the default `0`. ## fixedSize: refuse to adapt `fixedSize()` fixes a view at its ideal size in both dimensions. Apple calls it a counter-proposal to the size the parent offered. Two forms are useful: | Modifier | Width | Height | Typical use | |---|---|---|---| | `fixedSize()` | ideal | ideal | a short badge or price that must never truncate | | `fixedSize(horizontal: true, vertical: false)` | ideal | as proposed | a one-line label kept on a single line | | `fixedSize(horizontal: false, vertical: true)` | as proposed | ideal | a multi-line title that must show every line | The trade-off is spelled out in Apple's documentation: fixing a view at its ideal size **can result in the view exceeding the parent's bounds**. A fixed long title in a narrow row runs off the screen rather than truncating. ## Choosing between them 1. If one sibling matters more, raise its `layoutPriority(_:)`; the others still adapt, so nothing overflows. 2. If a view must keep its natural size and is known to be short, `fixedSize()` is simpler; review it whenever the content can grow. 3. If a multi-line title is being cut off vertically by a height-constrained parent, fix only the vertical axis so it can wrap to the width it is given but keep all its lines. 4. If the design wants a hard cap on lines, that is a text-rendering decision handled with the text modifiers for line limits and truncation, not a layout one. ## Spacer in the same row The `Spacer` between the two texts is itself a flexible child. `Spacer(minLength:)` sets the minimum length it can shrink to; `Spacer(minLength: 0)` lets it disappear entirely when space is tight, so it does not hold back points the title could use. ## Pitfalls interviewers listen for - Treating `layoutPriority` as a drawing order; stacking order is `zIndex(_:)`. - Assuming `fixedSize()` keeps text inside its container; it explicitly may not. - Reaching for a hard-coded `frame(width:)` on the price, which breaks with localisation and Dynamic Type. - Raising every view's priority, which restores the even split you started with. ## Testing the fix A truncation fix is only proven under the conditions that cause truncation: 1. Run the row at the largest Dynamic Type sizes, where every string's ideal width grows. 2. Try long strings, such as a long product name or a locale whose words are much longer than English. 3. Check the narrowest device the app supports, and iPad multitasking widths if they apply. A `fixedSize()` that looked harmless with a short English title is the change most likely to overflow in these runs.
- Why might fixedSize() on a long title make the screen worse than truncation?`fixedSize()` pins the title at its ideal size, which for text is the width of the whole string on one line. The parent cannot shrink it, so in a narrow row it extends beyond the row's bounds and off the screen, overlapping the price instead of truncating politely.
- What does fixedSize(horizontal: false, vertical: true) do to a multi-line Text?It leaves the width under the parent's control and fixes only the height at the ideal value. The text wraps to the width it is offered and takes as many lines as it needs, which stops a height-constrained parent from cutting it off with an ellipsis.
- If both texts in the row get layoutPriority(1), what happens?Priority is relative, so between the two texts equal priorities behave like two defaults: neither is satisfied first and they share the shortfall again. Raise only the view whose content matters most, or give the views distinct values.
- layoutPriority sets which sibling draws on top of the others.
- fixedSize() keeps text inside its parent's bounds.
- The default layoutPriority is 1, so you need 2 to beat it.
- A fixed frame width on the price is the robust fix.
- fixedSize(horizontal: false, vertical: true) lets the text grow wider than the row.