skip to content

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%

answer

  1. who decides the child's size
  2. an image's natural size
  3. the frame is laid out, the pixels spill
  4. resizable, then fit or fill

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.

solid answer

~50 s

The frame did not fail: a frame only **proposes** 300 by 200 and reports that size to its parent. The child decides, and an `Image` without `resizable()` always answers with the image's own size. So the layout reserves 300 by 200, siblings arrange around that box, and the frame centres the full-size image inside it, letting it draw beyond the edges because nothing clips by default. The fix is to make the image accept the proposal: call `resizable()`, then `aspectRatio(contentMode: .fit)` or `scaledToFit()` to keep the proportions, or `.fill` plus `clipped()` to cover the box and trim the overflow. The same diagnosis covers any view that seems to ignore its frame: find the child that answers with a size of its own, such as a non-resizable image or a view wrapped in `fixedSize()`, and check it with a temporary border.

code

swift · 17 lines
swift
import SwiftUI

struct ProductHero: View {
    let imageName: String

    var body: some View {
        VStack(alignment: .leading, spacing: 8) {
            Image(imageName)
                .resizable()
                .aspectRatio(contentMode: .fill)
                .frame(width: 300, height: 200)
                .clipped()
            Text("Linen Overshirt")
                .font(.title2)
        }
    }
}

go deeper

for a junior

Recall that an Image is not resizable by default, so it draws at its own size until you call resizable() and set an aspect ratio.

for a middle

Explain that the frame reports 300 by 200 to layout while the non-resizable image keeps its own size, so siblings sit around the box and the picture spills over them.

for a senior

Show the diagnosis, not just the fix: two temporary borders around the frame prove which child overflowed, and the same method finds fixedSize and nested frames that override a proposal.

for a principal

Turn the bug into a component rule: a shared remote-image or hero view that always applies resizable, a declared content mode and clipping, so individual screens cannot reintroduce overflowing artwork.

## The symptom A product detail screen shows a hero photo: ```swift Image("hero") .frame(width: 300, height: 200) .border(Color.blue) ``` The blue border is 300 by 200, exactly as asked, yet the photograph draws at its full size, covering the title below it and running off the edges. It looks as if SwiftUI ignored the frame. It did not. ## Why the frame "loses" SwiftUI layout is a negotiation: a parent **proposes** a size, the child **chooses** its own, and the parent **places** it. `frame(width:height:)` wraps the image in a new frame view that does three things: 1. It proposes 300 by 200 to the image. 2. It reports 300 by 200 to its own parent, whatever the image chose. Siblings in the surrounding stack are laid out around this box. 3. It places the image inside that box using its `alignment`, which defaults to `.center`. An `Image` that has not been made resizable answers every proposal with the image's own size. Apple's article on fitting images into available space describes exactly this case: the image data loads at full size, and because the frame is smaller, the image extends beyond the frame's boundaries. Nothing clips by default, so the overflow is drawn over neighbouring views even though layout treated the image as 300 by 200. ## The fix | Goal | Modifiers on the `Image` | |---|---| | Whole photo visible, letterboxed | `.resizable()` then `.aspectRatio(contentMode: .fit)` (or `.scaledToFit()`) | | Box fully covered, edges trimmed | `.resizable()` then `.aspectRatio(contentMode: .fill)`, frame, then `.clipped()` | | Stretch to the box, distorting | `.resizable()` alone | ```swift Image("hero") .resizable() .aspectRatio(contentMode: .fill) .frame(width: 300, height: 200) .clipped() ``` Three points to state in an interview: - `resizable(capInsets:resizingMode:)` is a method on `Image` that returns an `Image`, so it must come **before** any view modifier such as `frame`. - `resizable()` alone scales each axis independently; the aspect-ratio modifier restores the photo's proportions. - With `.fill`, the scaled image is larger than the box on one axis, so it overflows again until `clipped()` trims it. Fit, cropping and image rendering options are covered in depth with SwiftUI images. ## Diagnosing any "ignored" frame The image is the classic case, but the pattern is general. When a view seems to ignore a frame, look for a child that answers with a size of its own: - **A non-resizable `Image`** answers with the image's size. - **A view wrapped in `fixedSize()`** is fixed at its ideal size; Apple warns this can exceed the parent's bounds. - **A `Text` in a frame too small for it** first wraps, tightens, scales or truncates, but Apple notes it can still extend beyond the bottom of a frame that is too short. - **A nested fixed frame** inside the view proposes its own size and wins over the outer one for the content. ## Why SwiftUI behaves this way The behaviour follows directly from who owns the decision. In SwiftUI the child always chooses its size, so a frame can only ever make an offer. Scaling content is something each view type opts into: shapes accept any offer, `Text` reflows, and an `Image` scales only after `resizable()`. Without that call, an image's answer to every proposal is the same, which is what makes a photo look as if it ignores every frame you try. The fix is therefore always on the child, never a stronger frame around it. ## How to prove it - Add a temporary `border(_:width:)` before **and** after the frame, in two colours. If the inner border is larger than the outer one, the child overflowed the proposal. - Select the view in an Xcode preview to see its bounds outlined. - Remove modifiers one at a time from the bottom of the chain and watch which change moves the inner border. ## What not to do - Swapping to `frame(maxWidth: 300, maxHeight: 200)` does not help: the image still answers with its own size. - Adding `fixedSize()` does not help either: it pins the image at its ideal size, which is the full image. - Measuring the screen with a `GeometryReader` to compute a size is unnecessary; the image only needs to accept the proposal it already receives.

  • Why does the title below the image not move down when the image overflows?
    Layout uses the size the frame reported, 300 by 200, not the size the image drew at. The `VStack` places the title after a 200-point-tall box, and the oversized image is centred in that box and drawn beyond it. Overflow is a drawing effect; it does not feed back into the sizes siblings were given.
  • Would frame(maxWidth: 300, maxHeight: 200) have stopped the overflow?
    No. A flexible frame still only proposes; a non-resizable image answers with its own size regardless of the bounds. So the picture still draws at its full size whichever frame wraps it. The image must be made resizable before any frame can size it.
  • When does aspectRatio with .fill still need clipped()?
    Whenever the box's proportions differ from the photo's. `.fill` scales the image until it covers the whole frame, which leaves it larger than the frame on one axis. Without `clipped()`, that excess is drawn outside the frame, so the image overlaps neighbouring views exactly as before.

  • The frame is broken, so the image needs a GeometryReader to read the screen size.
  • Adding fixedSize() to the image will make it respect the frame.
  • resizable() can go after frame because modifier order does not matter.
  • An overflowing image pushes its siblings away to make room for itself.
  • aspectRatio(contentMode: .fill) keeps the image inside the frame automatically.