Background images in SwiftUI

SwiftUI has no background-image property, but it has two good ways to put a picture behind a view. Here is which one to use, and how to keep the image full-screen, sharp and light on memory.

9 min read
Smartphone with a crumpled amber paper texture filling its screen, lying on dark slate
In short
  • Attach the image with the .background modifier. It takes the size of your content, so a large image cannot push the layout off the screen.
  • For a full-screen image, call .resizable(), then .ignoresSafeArea(), then .scaledToFill(). The order matters.
  • A full-screen image on a 3x iPhone decodes to about 12 MB. Ship 2x and 3x variants and downsample remote files.
  • Put a Dark appearance variant in the asset catalog. iOS swaps the file for you.
12 MBDecoded size of one full-screen image on a 3x iPhone
2Content modes in SwiftUI: fit and fill
4.5:1Minimum contrast for body text (WCAG AA)

SwiftUI draws a background the same way it draws everything else: as a view. You put an Image behind your content, either with the .background modifier or at the bottom of a ZStack. This guide shows both, then covers what decides whether the result looks right on every iPhone: safe areas, scaling, memory, overlays, lists, dark mode and the launch screen.

The checks are the same whether your own team writes the screens or you hire an iOS app development agency. Every background has a pixel size, a memory cost and a contrast problem, and you can measure all three before release.

If you have read our guides to background images in Flutter or React Native, the budgets will look familiar. Only the modifiers change.

Should you use .background or a ZStack?

Start with .background. Use a ZStack only when the image has to take part in the layout or be animated on its own.

Need.background modifierZStack
Who decides the sizeThe content in frontThe largest child, often the image
Static full-screen or card backgroundYes, simplest codeWorks, but can widen the layout
Tint or gradient overlay.overlay on the imageSeparate layer
Parallax, transitions, swapping imagesPossibleEasier
Rounded card corners.clipShape on the view.clipShape on the stack

The ZStack trap is real. A ZStack sizes itself to its largest child. An image with .scaledToFill() on a portrait screen grows wider than the screen, the stack grows with it, and your centered text slides off the edges. With .background, the content sets the size and the image only fills it.

How do you set a background with .background?

Give the screen's root view a .background with a resizable image inside. Since iOS 15 the modifier takes a view builder, which reads cleanly:

VStack {
    Text("Welcome")
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background {
    Image("bg-kraft")
        .resizable()
        .ignoresSafeArea()
        .scaledToFill()
}

Three details catch people out:

  1. Add the file to the asset catalog. Drag it into Assets.xcassets and use the asset name, not the file name with an extension. A wrong name gives an empty view and a console warning.
  2. Make the content full-size. A VStack is only as big as its children. Add .frame(maxWidth: .infinity, maxHeight: .infinity) so the background has a full screen to fill.
  3. Call .resizable() first. Without it the image shows at its natural pixel size and ignores every scaling modifier after it.

How do you make the image fill the whole screen?

Call .ignoresSafeArea() on the image before .scaledToFill(). The safe area keeps content clear of the Dynamic Island and the home indicator, but a background should run under both.

Modifier order decides the result. If .scaledToFill() comes first, the image is scaled to the smaller safe-area box, and only then expanded, which can leave a gap at the bottom of the screen. Put .ignoresSafeArea() first and the image is scaled to the full screen. A developer forums thread on Image and ignoresSafeArea walks through exactly this case.

Stop the jump when the keyboard opens

On a login screen, the keyboard shrinks the safe area and a fitted background can rescale. Plain .ignoresSafeArea() covers all regions, the keyboard included, so the image stays put. If you passed .container only, add .keyboard. The older .edgesIgnoringSafeArea(.all) is deprecated, so replace it when you touch that code.

Which scaling should a background use?

Use .scaledToFill() for almost every full-screen background. It keeps the aspect ratio, fills the frame and lets the overflow be cropped. SwiftUI has only two content modes, so the options are short:

ModifiersWhat it doesUse it for
.resizable().scaledToFill()Fills the frame, keeps the ratio, crops the restFull-screen and card backgrounds
.resizable().scaledToFit()Shows the whole image, leaves empty bandsArtwork that must not be cropped
.resizable() aloneStretches width and height separatelyFlat colors and soft gradients only
.resizable(resizingMode: .tile)Repeats the image at its own sizeSmall seamless textures
No .resizable()Natural size, centeredSmall marks, rarely backgrounds

Plan for the portrait crop

An iPhone screen is roughly 9:19.5. A 4:3 file, such as our 1600×1200 backgrounds, loses most of its left and right sides on a portrait screen. Keep the important part near the center, or choose a pattern with no focal point. Calm surfaces like the kraft paper texture or the linen fabric texture survive any crop.

Tile only true seamless textures

.resizable(resizingMode: .tile) repeats the image at its point size, so a 512-pixel tile in the 2x slot repeats every 256 points. Edges that do not match show as a grid. Our guide to seamless tileable textures explains how to check a tile before you use it.

How big should the image be?

Make the file as large as the biggest screen you support, in physical pixels, and no larger. The size on disk is not the real cost. A decoded image takes 4 bytes per pixel, whatever the format.

ImagePixelsDecoded in memory
3x iPhone screen (393×852 points)1179×2556about 12 MB
Web Backgrounds file1600×1200about 7.7 MB
4000×3000 camera original4000×3000about 48 MB
512×512 seamless tile512×512about 1 MB

Ship 2x and 3x variants

An image set in the asset catalog has 1x, 2x and 3x slots. Current iPhones use 2x or 3x, and the app gets only the variant its device needs when it is installed from the App Store. For flat vector shapes, a single PDF or SVG with "Preserve Vector Data" turned on also works. Photos and textures should stay bitmaps.

Downsample remote and user images

For images that do not come from the catalog, decode a smaller copy before display. Since iOS 15, UIImage does it in one call: byPreparingThumbnail(ofSize:) in async code, preparingThumbnail(of:) elsewhere.

@Environment(\.displayScale) private var displayScale

// size comes from the layout, for example a GeometryReader
let target = CGSize(width: size.width * displayScale,
                    height: size.height * displayScale)
let small = await original.byPreparingThumbnail(ofSize: target)

Read the size from the layout instead of hard-coding one phone. On older targets, ImageIO's CGImageSourceCreateThumbnailAtIndex with kCGImageSourceThumbnailMaxPixelSize gives the same result.

How do you keep text readable on top?

Darken the image under the text. One overlay line is enough:

Image("bg-kraft")
    .resizable()
    .ignoresSafeArea()
    .scaledToFill()
    .overlay(Color.black.opacity(0.45))

A black layer at 40 to 55 percent is a common start for white text on a busy texture. When the text sits only at the bottom, use a LinearGradient from clear to black instead, so the rest of the image stays bright. For a small panel, .background(.ultraThinMaterial) gives the system frosted look and adapts to dark mode.

The target is the WCAG contrast minimum: 4.5:1 for body text and 3:1 for large text. Measure it on the brightest area that can sit behind text, not on the average. Our article on readable text on background images shows how.

Mark the image as decorative with Image(decorative:) or .accessibilityHidden(true), so VoiceOver skips it.

Why is the background hidden behind a List or Form?

List and Form draw their own opaque background on top of yours. Since iOS 16, hide it with .scrollContentBackground(.hidden):

List(items) { item in
    Text(item.title)
}
.scrollContentBackground(.hidden)
.background {
    Image("bg-slate")
        .resizable()
        .ignoresSafeArea()
        .scaledToFill()
}

Rows keep their own fill. Set .listRowBackground(Color.clear) or a translucent material if the texture should show through them too. In a NavigationStack, apply the background to the content view inside the stack, not to the stack itself, and use .toolbarBackground(.hidden, for: .navigationBar) if the bar should not cover the image.

How do you switch backgrounds for dark mode?

Let the asset catalog do it. Select the image set, set Appearances to "Any, Dark", and drop the dark file into the Dark slot. Image("bg-kraft") then loads the right file and updates the moment the user switches mode. No code is needed.

If the choice depends on app state rather than the system setting, read @Environment(\.colorScheme) and pick the asset name in code. In both cases, a dark variant is not the light file with the brightness turned down. Lower the contrast inside the texture, remove pure white highlights and keep the tone near your dark surface color. Our notes on using backgrounds in dark mode cover the visual side, and the dark slate stone texture is a ready dark base.

Should backgrounds be bundled or loaded from a URL?

Bundle the backgrounds that appear on first launch and on core screens. They work offline and are ready on the first frame.

Load from a URL when backgrounds change often, such as seasonal themes from a CMS. Then:

  1. Use AsyncImage with a phase closure, and show a solid color in the empty phase. It avoids a white flash.
  2. Serve a file sized for phones. A 4000-pixel original wastes data and memory.
  3. Keep a cache of your own. AsyncImage does not hold the decoded image when the view goes away, so a background that appears on many screens is fetched and decoded again.
  4. Downsample before display, as shown above.

What about the launch screen?

Keep the launch screen plain and show the rich background on the first real screen. In SwiftUI apps the launch screen is set in Info.plist under UILaunchScreen, with keys such as UIColorName and UIImageName. It is static: no code runs, and no text should appear on it.

Apple's guidelines for launching ask for a launch screen that looks like the first screen of the app, not a branded splash. A pattern that works:

  1. Launch screen: a solid color taken from the darkest tone of your background.
  2. First screen: the same color under the image, so the image appears without a flash.
  3. Onboarding: one background per step at most, all from the same family of textures.

Our guide to splash screen backgrounds covers the same hand-off on Android.

A checklist before you ship

  • Backgrounds are attached with .background, and a ZStack is used only where the image must drive layout or animate.
  • Every image call starts with .resizable(), then .ignoresSafeArea(), then .scaledToFill().
  • The key area of the image survives a portrait crop.
  • Image sets have 2x and 3x variants, and remote images are downsampled.
  • Text on top reaches 4.5:1 contrast on the brightest part of the image.
  • The image is decorative for VoiceOver.
  • Lists and forms use .scrollContentBackground(.hidden).
  • Dark mode loads its own file from the Dark slot, not a dimmed copy.
  • The launch screen color matches the first screen.

Need a starting point? Browse background textures for calm surfaces that work behind text, or read background images in React Native if part of your app is cross-platform.

Questions about SwiftUI backgrounds

How do I set a background image in SwiftUI?

Add a .background modifier to your root view and put an Image inside it with .resizable(), .ignoresSafeArea() and .scaledToFill(). Give the root view a full-size frame so the image has the whole screen to fill.

Why does my SwiftUI background image leave a gap at the bottom?

The modifiers are in the wrong order. Call .ignoresSafeArea() before .scaledToFill(), so the image is scaled to the full screen and not to the smaller safe-area box.

Why does my text go off screen when I use a ZStack background?

A ZStack takes the size of its largest child. A filled image on a portrait screen is wider than the screen, so the stack and its text widen too. Use the .background modifier, where the content sets the size.

How do I show a background image behind a List in SwiftUI?

Add .scrollContentBackground(.hidden) to the List or Form, available since iOS 16, then attach your image with .background. Set .listRowBackground(Color.clear) if the rows should be see-through as well.

What resolution should a SwiftUI background image be?

Match the largest screen you support in physical pixels, about 1179 by 2556 for a 3x iPhone. Put 2x and 3x variants in the asset catalog and downsample remote images before display.

Keep reading

Built for the web.
Free to download.