Measuring Subviews
Query subview sizes during layout.
Measuring Subviews is a free Swift Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Measurement Drives Layout
Before you can place children, you must measure them. SwiftUI layout is a negotiation: the parent proposes sizes, children respond, and you reconcile the answers.
Asking a Subview Its Size
Each proxy in Subviews answers sizeThatFits(_:) for a given ProposedViewSize. The proxy returns the size that child would take under that proposal.
let size = subview.sizeThatFits(.unspecified)The Three Probe Proposals
To learn a child’s flexibility, probe it three ways: .zero reveals its minimum, .infinity its maximum, and .unspecified its ideal size.
let minSize = subview.sizeThatFits(.zero)
let maxSize = subview.sizeThatFits(.infinity)
let ideal = subview.sizeThatFits(.unspecified)Proposing a Specific Width
For wrapping text, propose a concrete width and leave height nil. The child reports the height it needs at that width.
let proposal = ProposedViewSize(width: 200, height: nil)
let fit = subview.sizeThatFits(proposal)Layout Priority
Each subview exposes a priority (set by .layoutPriority). When space is tight, give higher-priority children their ideal size first.
for subview in subviews {
let p = subview.priority
// distribute space to higher priority first
}Reading Custom Values
Subviews can carry typed data via a LayoutValueKey. The layout reads subview[MyKey.self] to influence placement—for example a per-item spacing.
private struct RankKey: LayoutValueKey {
static let defaultValue: Int = 0
}
let rank = subviews[0][RankKey.self]Setting a Layout Value
A view sets its layout value with the layoutValue(key:value:) modifier, letting children communicate hints to your layout.
Text("Featured").layoutValue(key: RankKey.self, value: 10)Spacing Between Subviews
Subviews expose spacing preferences. Use subview.spacing.distance(to:along:) to get the recommended gap between two adjacent children along an axis.
let gap = subviews[0].spacing.distance(
to: subviews[1].spacing, along: .horizontal)Accumulating Sizes
A horizontal layout sums child widths and takes the tallest height. Measuring all children first lets you compute the container size before placing anything.
let sizes = subviews.map { $0.sizeThatFits(.unspecified) }
let totalWidth = sizes.reduce(0) { $0 + $1.width }
let maxHeight = sizes.map(\.height).max() ?? 0Re-Measuring During Placement
In placeSubviews you must re-measure (or reuse cached sizes) so positions match what you reported. Measurements are cheap proxy calls, not real renders.
func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {
for s in subviews {
let size = s.sizeThatFits(.unspecified)
// use size to position s
_ = size
}
}Respecting the Parent Proposal
Always honor the proposal you receive. If the parent proposes a fixed width, lay children out within it; do not silently overflow, or the layout will clip.
let containerWidth = proposal.width ?? .infinityQuick Check: Measuring
Test your understanding of subview measurement.
Recap: Measuring Subviews
Measurement is the heart of custom layout. Probe children with .zero, .infinity, and .unspecified to learn min/max/ideal sizes, propose concrete widths for wrapping content, and respect layout priority and spacing preferences.
Custom LayoutValueKey values let children pass hints. Measure consistently in both protocol methods so reported size and placement always agree.
Frequently asked questions
Is the “Measuring Subviews” lesson free?
Yes — the full text of “Measuring Subviews” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.
What will I learn in “Measuring Subviews”?
Query subview sizes during layout. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Measuring Subviews” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.