Skip to content

How to use and ignore the Safe Area in SwiftUI | SwiftUI Bootcamp #17

The safe area is iOS's guarantee that your content won't be obscured by the notch, Dynamic Island, status bar, or home indicator. After this lesson you'll know how to work within the safe area by default, how to strategically extend backgrounds behind system chrome, and the difference between the old edgesIgnoringSafeArea and the modern ignoresSafeArea.

What You'll Learn

  • What the safe area is, where it comes from, and why SwiftUI respects it by default
  • How .ignoresSafeArea(edges:) extends a background behind the status bar or home indicator
  • The correct pattern: let content respect the safe area while extending only background colors/images beyond it

Mental Model

Think of the safe area like the matte around a painting in a frame. The painting (your content) is inset from the frame edges so it's not obscured. The wall behind the frame (the background color) extends all the way to the physical edges. In iOS terms: the status bar is the frame edge at the top, the home indicator is the frame edge at the bottom. Your text, buttons, and interactive content should stay inside the matte (safe area). But your background colors and decorative gradients can extend behind the frame.

This is the key insight: .ignoresSafeArea() should almost always be applied to background views, not to your content views. Content that runs under the notch is unreadable and inaccessible. A background that runs under the notch creates a seamless, immersive look.

Detailed Explanation

SwiftUI automatically insets all content within the safe area. On iPhone 14 Pro, that means content starts below the Dynamic Island and above the home indicator. On iPhone SE, it starts below the status bar. This automatic behavior protects content from system chrome.

.ignoresSafeArea(edges: .top) tells a view to extend into the top safe area (behind the status bar and notch/Dynamic Island). .ignoresSafeArea(edges: .all) extends into all safe areas. The most common use is extending a background color or image so there's no ugly white gap behind system chrome.

The deprecated edgesIgnoringSafeArea(.all) was the UIKit-era API and is still present for backward compatibility, but .ignoresSafeArea(edges:) is the modern replacement. The modern API also accepts a second parameter for the safe area region (.container, .keyboard, .all) — edges: is the specific form for edge-based safe areas.

The pattern in the sample is a common and correct one: a ScrollView with a content VStack scrolls normally within the safe area, but the .background() — applied to the ScrollView — ignores the top safe area so the red color extends behind the status bar.

Code Structure

The sample is in SafeAreaBootcamp.swift and shows a vertically scrolling list of cards with a title. The ScrollView has a red background that ignores the top safe area (extending behind the status bar), while the scroll content itself respects the safe area. The commented-out code shows an alternative ZStack approach.

Complete Code

SafeAreaBootcamp.swift

swift
import SwiftUI

struct SafeAreaBootcamp: View {
    var body: some View {
        
        ScrollView {
            VStack {
                Text("Title goes here")
                    .font(.largeTitle)
                    .frame(maxWidth: .infinity, alignment: .leading) // left-aligned, full-width title
                                
                ForEach(0..<10) { index in
                    RoundedRectangle(cornerRadius: 25.0)
                        .fill(Color.white)   // white card contrasts with the red background
                        .frame(height: 150)  // fixed card height
                        .shadow(radius: 10)  // drop shadow for card depth
                        .padding(20)         // spacing between cards
                }
            }
        }
        //.background(Color.blue)  // plain background — would stop at the safe area edge
        .background(
            Color.red
                //.edgesIgnoringSafeArea(.all)  // deprecated API — works but avoid in new code
                .ignoresSafeArea(edges: .top)   // extends red background behind status bar / Dynamic Island
        )
        
        
        
        
        
        
        
        
//        ZStack {
//            // background
//            Color.blue
//                .edgesIgnoringSafeArea(.all)  // alternative: full-screen color behind everything
//
//            // foreground
//            VStack {
//                Text("Hello, World!")
//                Spacer()
//            }
//            .frame(maxWidth: .infinity, maxHeight: .infinity)
//            .background(Color.red)
//        }

    }
}

struct SafeAreaBootcamp_Previews: PreviewProvider {
    static var previews: some View {
        SafeAreaBootcamp()
    }
}

Code Walkthrough

  1. ScrollView { VStack { ... } } — The scroll view contains the actual content (title + cards). This content scrolls within the safe area — the title and cards never slide under the status bar when you scroll down. SwiftUI handles this automatically.
  2. .frame(maxWidth: .infinity, alignment: .leading) — Makes the title span full width and left-aligns it. Without this, the title would be centered and only as wide as its text.
  3. ForEach(0..<10) with RoundedRectangle — Generates 10 white cards to give the scroll view enough content to scroll through. The white cards provide strong contrast against the red background.
  4. .background(Color.red.ignoresSafeArea(edges: .top)) — This is the core of the lesson. The .background() is applied to the ScrollView, making red the background of the entire scroll view. .ignoresSafeArea(edges: .top) on the Color.red causes the red color to extend behind the top safe area (status bar, Dynamic Island). The scroll content is unaffected and still respects the safe area.
  5. The commented ZStack alternative — An equivalent approach using ZStack: place a full-bleed Color.blue as the first (back) child, then place the content VStack on top. Both approaches produce the same visual — choose whichever is clearer in context.

Common Mistakes

Mistake: Applying .ignoresSafeArea() to the content view instead of the background If you apply .ignoresSafeArea(edges: .top) to the VStack containing your title and cards, the content shifts up behind the status bar — the title is obscured and unreachable. Always apply safe area ignoring to background views (colors, gradients, images), not to content views.

Mistake: Using edgesIgnoringSafeArea(.all) in new code This API still works but was deprecated in iOS 14. Use .ignoresSafeArea(edges: .all) instead. The new API also supports .ignoresSafeArea(.keyboard) to prevent the keyboard from shrinking your layout — something the old API couldn't do.

Mistake: Expecting .ignoresSafeArea() to also move scrollable content behind the status bar.ignoresSafeArea() on a scroll view's background extends the background color behind system chrome, but the scroll view's content inset is unaffected. SwiftUI automatically insets scrollable content so it starts below the status bar. If you want content to start above the safe area (unusual, and almost always wrong for text), you'd need to modify the safe area insets explicitly.

Key Takeaways

  • SwiftUI respects the safe area by default — content never starts behind the status bar or home indicator without explicit override
  • Apply .ignoresSafeArea(edges:) to background views (colors, gradients), not to content views
  • .ignoresSafeArea(edges: .top) is the modern replacement for the deprecated .edgesIgnoringSafeArea(.all)

Last updated: June 27, 2026

Released under the MIT License.