skip to content

Size Proposals and Frames

A parent proposes a size, the child picks its own and the parent places it; frame, fixedSize and layoutPriority all act on that exchange. Interviewers ask why a view ignores a frame.

on this pageshow

explore

questions

6

In SwiftUI, what is the difference between frame(width:height:) and frame(maxWidth: .infinity), and when do you use each?

level: juniorimportance: must knowfreq 72%

answer

  1. one is rigid, one stretches
  2. the frame grows, the text does not
  3. nil keeps the child's sizing
  4. alignment places the child inside

basics

~20 s

frame(width:height:) makes a fixed-size frame that reports exactly those dimensions whatever its child picks. frame(maxWidth: .infinity) makes a flexible frame that grows to the width its parent offers, so a background or button can span the row.

solid answer

~40 s

Both calls wrap the view in an invisible frame view; they differ in how that frame sizes itself. `frame(width:height:)` is a **fixed** frame: it proposes the given size to its child and reports that size upward, and any dimension you leave `nil` keeps the child's own sizing. `frame(minWidth:idealWidth:maxWidth:minHeight:idealHeight:maxHeight:alignment:)` is a **flexible** frame: with `maxWidth: .infinity`, when the parent offers more width than the child needs, the frame takes the whole offer, while the child inside still picks its own width and is placed by the frame's `alignment`, centred by default. Use a fixed frame for things that really have a set size, such as an icon or avatar; use `maxWidth: .infinity` to make a full-width button, stretch a row's background, or push a label to the leading edge with `alignment: .leading`.

code

swift · 18 lines
swift
import SwiftUI

struct BuyBox: View {
    var body: some View {
        VStack(spacing: 12) {
            Text("Save")
                .frame(width: 120, height: 44)
                .background(Color.gray.opacity(0.2))
            Text("Add to Bag")
                .frame(maxWidth: .infinity, minHeight: 44)
                .background(Color.blue)
                .foregroundStyle(.white)
            Text("Ships in 2 days")
                .frame(maxWidth: .infinity, alignment: .leading)
        }
        .padding()
    }
}

go deeper

for a junior

Recall that frame(width:height:) is fixed and frame(maxWidth: .infinity) is flexible, and that both wrap the view in a new frame view rather than changing the view itself.

for a middle

Explain the flexible-frame rules: with only a maximum, the frame takes the offer when it exceeds what the child needs, and the child is still placed by the frame's alignment inside that area.

for a senior

In review, question fixed heights on text-bearing controls and suggest minHeight, so the screen survives large Dynamic Type sizes without clipped or overflowing labels.

for a principal

Set the team default: flexible frames and alignment for layout, fixed frames only for genuinely fixed artwork, so screens adapt to new devices without per-device constants.

## Two frame modifiers, one idea A SwiftUI frame is not a property of a view. Each `frame` call wraps the view in a new, invisible **frame view** that takes part in layout: it receives a proposal from its parent, proposes something to its child, chooses its own size and places the child inside itself. The two families differ only in how the frame chooses. | | `frame(width:height:alignment:)` | `frame(minWidth:idealWidth:maxWidth:...)` | |---|---|---| | Kind | fixed | flexible | | Frame's own size | exactly the given values | computed from the proposal, the child and the bounds | | Proposal to the child | the given values | the parent's proposal, clamped to the bounds | | Typical use | icons, avatars, a fixed-height banner | full-width buttons, row backgrounds, leading alignment | Both are available on every SwiftUI release (iOS 13 and later), and both default `alignment` to `.center`. ## The fixed frame `frame(width:height:alignment:)` gives the resulting view fixed dimensions for every non-`nil` argument. If you pass only one dimension, Apple's documentation says the resulting view assumes the child's sizing behaviour in the other. Two consequences matter in practice: - The frame's size does **not** depend on the child. Siblings lay out around the frame, not around the content. - The content does **not** have to fit. Apple notes that a `Text` taller than its frame may extend beyond the frame's bounds, and a non-resizable image keeps its full size. ## The flexible frame For each dimension, the flexible frame follows the rules Apple documents: 1. With no minimum and no maximum, the frame adopts the child's size in that dimension. 2. With both a minimum and a maximum, the frame takes the proposed size, clamped to them. 3. With only a maximum, if the parent proposes more than the child needs, the frame takes the proposal clamped to the maximum; otherwise it takes the child's size. 4. With only a minimum, if the parent proposes less than the child needs, the frame takes the proposal clamped to the minimum; otherwise it takes the child's size. So `maxWidth: .infinity` means "take whatever width is offered". The child is offered that width too, but a `Text` still chooses only what its string needs. The frame then aligns the text inside the wider area. That is why the background must come **after** the frame for it to span the row: the background sizes itself to the view it is attached to, a point covered fully with modifier ordering. An `idealWidth` or `idealHeight` replaces an unspecified (`nil`) dimension in the proposal the frame passes down, which matters when something asks the view for its ideal size, for example `fixedSize()`. ## A product detail screen ```swift struct BuyBox: View { var body: some View { VStack(spacing: 12) { Text("Save") .frame(width: 120, height: 44) .background(Color.gray.opacity(0.2)) Text("Add to Bag") .frame(maxWidth: .infinity, minHeight: 44) .background(Color.blue) .foregroundStyle(.white) Text("Ships in 2 days") .frame(maxWidth: .infinity, alignment: .leading) } .padding() } } ``` - **Save** is always 120 by 44, on an iPhone mini or an iPad. - **Add to Bag** spans the width the `VStack` offers, and is at least 44 points tall. - **Ships in 2 days** sits at the leading edge, because the flexible frame is full width and its alignment places the text on the left in a left-to-right locale. ## Choosing between them - Prefer flexible frames for layout that should adapt to device size, orientation and Dynamic Type. - Keep fixed frames for things that truly have a fixed size, such as an icon, a thumbnail slot or a tap target. - Use `minHeight` rather than `height` for tap targets that contain text, so larger text sizes can grow the row instead of overflowing it. - Reach for `frame(maxWidth: .infinity, alignment: .leading)` before adding a `Spacer` just to push one view to an edge; it states the intent in one modifier. ## Mistakes reviewers catch - **A fixed height on a text button.** At the largest accessibility text sizes the label no longer fits and draws outside the button; `minHeight` lets the frame grow with the text. - **Expecting `alignment` to move the frame.** The parameter positions the child inside the frame; where the frame itself sits is decided by its own parent. - **Both bounds set by accident.** With a minimum and a maximum in one dimension, the frame unconditionally takes the proposed size clamped to those bounds, so it no longer hugs its content at all.

  • What happens to the width when you call frame(height: 100) without a width?
    The frame fixes only the height. In the width dimension the resulting view assumes the child's own sizing behaviour, so an `Ellipse` still takes the width it is offered and a `Text` still takes the width its string needs. A fixed frame never invents a value for a dimension you left `nil`.
  • Why does a short Text inside frame(maxWidth: .infinity) still look centred rather than stretched?
    The frame expands to the offered width, but the `Text` inside it chooses only the width its string needs. The frame then places the text using its `alignment`, which defaults to `.center`. Pass `alignment: .leading` to move it, or put a background after the frame to make the full width visible.

  • frame(maxWidth: .infinity) stretches the Text itself to the full width.
  • frame(width: 200) guarantees the content fits inside 200 points.
  • A fixed frame needs both a width and a height to have any effect.
  • The frame's alignment parameter moves the frame within its parent.
  • Use fixed frames everywhere so layouts look identical on every device.
open as a page

In SwiftUI, how does a view end up with its size, and what happens in the propose, choose and place exchange between parent and child?

level: middleimportance: must knowfreq 68%

basics

~20 s

A SwiftUI parent proposes a size to each child, the child chooses its own size, and the parent positions it. A parent cannot impose a size; a modifier such as frame inserts a new parent that proposes differently.

open as a page

In SwiftUI, when a product title truncates or wraps unexpectedly beside a price in an HStack, how do fixedSize and layoutPriority change the result?

level: middleimportance: should knowfreq 55%

basics

~20 s

In 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.

open as a page

In SwiftUI, how do ignoresSafeArea and safeAreaInset differ, and which keeps a custom bottom bar clear of the home indicator?

level: middleimportance: should knowfreq 58%

basics

~20 s

In SwiftUI, ignoresSafeArea extends a view into the safe area, while safeAreaInset(edge:) places a view at an edge and insets the modified view to make room. Put a bottom bar in safeAreaInset; let only its background ignore the safe area.

open as a page

On a SwiftUI product detail screen, a hero Image given .frame(width: 300, height: 200) still draws at its full size; why does it ignore the frame, and how do you fix it?

level: seniorimportance: should knowfreq 52%

basics

~20 s

A SwiftUI Image that is not resizable keeps its natural size whatever the frame proposes, so the 300 by 200 box is laid out but the picture spills past it. Make it resizable, set an aspect ratio, and clip when filling.

open as a page

In SwiftUI, how does containerRelativeFrame size a view from its nearest container, for example product gallery cards that show one and a half per screen?

level: middleimportance: nice to knowfreq 32%

basics

~20 s

SwiftUI's containerRelativeFrame (iOS 17) sizes a view from its nearest container, such as the screen or a ScrollView, minus that container's safe area insets. Its count, span and spacing form splits that length into columns, so cards can peek.

open as a page