Why this happens
SwiftUI views don’t automatically know the size of the screen they’re rendered on. The layout system works inside-out: a view proposes a size to its children, and children report back their ideal size. When you need a child — like an image — to match the full screen width, you must explicitly read that width from the environment.
Two common approaches exist, but they serve different purposes. UIScreen.main.bounds returns the physical device screen size, ignoring safe areas, split-view on iPad, and any container that might be smaller than the full screen. GeometryReader reports the proposed size for the specific view hierarchy, making it the correct choice for most responsive layouts.
Choosing the wrong one leads to images that overflow on iPad multitasking, ignore the Dynamic Island, or break when embedded in a smaller container.
How to fix it
1. Use GeometryReader for layout-aware width (recommended)
GeometryReader injects the parent’s proposed size into the view builder. This is the idiomatic SwiftUI way to size a child relative to its container.
import SwiftUI
struct SquareImageView: View {
var body: some View {
GeometryReader { geometry in
let side = geometry.size.width
Image("someImage")
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: side, height: side)
.clipped()
}
}
}
Key points:
– The closure receives a GeometryProxy with size, safeAreaInsets, and frame(in:).
– geometry.size.width respects the current layout context — full screen, split view, or a custom frame.
– Wrap the content in the GeometryReader; the reader itself takes zero space unless you give it a frame.
When to use: Any time the view should adapt to its actual container — lists, sheets, popovers, iPad split view, or future form factors.
2. Use UIScreen for truly global metrics
If you genuinely need the physical screen bounds (e.g., a full-screen background that must cover the entire display regardless of safe area), extend UIScreen:
import UIKit
extension UIScreen {
static var screenWidth: CGFloat {
main.bounds.width
}
static var screenHeight: CGFloat {
main.bounds.height
}
}
Usage:
struct FullScreenBackground: View {
var body: some View {
Color.blue
.frame(width: UIScreen.screenWidth, height: UIScreen.screenHeight)
.ignoresSafeArea()
}
}
Caveats:
– Does not update on rotation unless you observe UIDevice.orientationDidChangeNotification and force a redraw.
– Ignores iPad multitasking, external displays, and safe areas.
– Hard to test in previews because UIScreen.main is tied to the running device.
When to use: Rare. Full-screen launch images, camera previews that must cover every pixel, or debugging overlays.
3. Prefer container-relative frames with .containerRelativeFrame (iOS 17+)
For simple full-width children, the modern API avoids GeometryReader boilerplate:
struct SquareImageView: View {
var body: some View {
Image("someImage")
.resizable()
.aspectRatio(contentMode: .fill)
.containerRelativeFrame(.horizontal) { length, _ in
length // width equals container width
}
.frame(height: nil) // height follows aspect ratio
.clipped()
}
}
.containerRelativeFrame reads the nearest container’s size, not the whole screen. It’s lighter than GeometryReader and works inside scroll views, grids, and custom layouts.
How to prevent it
- Default to
GeometryReaderor.containerRelativeFramefor any view that should size itself to its parent, not the device. - Reserve
UIScreen.main.boundsfor code that truly needs physical display metrics. - Test on iPad split view, iPhone with Dynamic Island, and landscape orientation — these expose hardcoded-screen assumptions.
- Use Xcode Previews with
.previewLayout(.fixed(width: 390, height: 844))and.previewLayout(.fixed(width: 1024, height: 768))to verify responsive behavior without a device.
FAQ
Does GeometryReader work inside a ScrollView?
Yes. The GeometryProxy reports the size of the scroll view’s content area, not the screen. If you need the visible frame, use geometry.frame(in: .global) or .scrollTargetLayout() with scrollPosition.
Why does my image still have extra space on the sides?
Check the parent view’s padding or safe-area insets. GeometryReader inherits the proposed size after the parent applies its own modifiers. Add .ignoresSafeArea() to the GeometryReader only if you intentionally want to extend under the notch or home indicator.
Can I get screen width in a ViewModel without passing GeometryProxy?
Not reliably. SwiftUI deliberately keeps layout information out of the data layer. If a ViewModel needs a dimension, pass it as a plain CGFloat from the view — this keeps the ViewModel testable and platform-agnostic.
Need a square image that always matches the container width? Wrap it in GeometryReader and set both width and height to geometry.size.width. For full-screen backgrounds only, UIScreen.main.bounds.width works — but test on iPad split view first.
Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.