Improve your agent's Swift and SwiftUI code with SwiftFairy's on-device reviews! Learn more ...Improve your agent's Swift and SwiftUI codewith SwiftFairy! Learn more...

Safe area and content margins in SwiftUI

The safe area plays an important role in the SwiftUI layout process, influencing how views are sized and positioned within their containers. As it propagates through the view hierarchy, containers and view modifiers can adjust it, changing the space available to descendant views. The way views use this space also varies, with scroll views resolving the safe area into margins for their content. Understanding how the safe area is propagated, adjusted and resolved can help us reason about our layouts and choose the appropriate APIs to modify them.

# Safe area propagation through the view hierarchy

By default, the SwiftUI layout system sizes and positions views within the safe area, the part of the window that isn't obscured by device features or interface bars. The root view of a scene is proposed the size of the safe area and placed within it. As layout proceeds down the view hierarchy, containers size and position their subviews within their own bounds, so views generally remain within the safe area unless they extend beyond the bounds of their containers.

Each view also has its own safe area insets, which, like its size and position, are part of its geometry. These insets aren't necessarily the same as those of the window and can be read from the safeAreaInsets property of the geometry proxy passed to the onGeometryChange(for:of:action:) modifier applied to the view.

IndigoRectangle()
    .onGeometryChange(for: EdgeInsets.self) { proxy in
        proxy.safeAreaInsets
    } action: { insets in
        print(insets.bottom)
    }

A view's inset on each edge depends on where that edge lies relative to the corresponding safe area boundary. If the edge lies inside the safe area, short of the boundary, the inset is zero. If it reaches or extends beyond the boundary, the inset is the remaining distance from the view's edge to the corresponding edge of the window.

Three views near the bottom safe area boundary: one 10 points short of it has a bottom inset of 0 points, one on the boundary has 34 points, and one 20 points past it has 14 points Three views near the bottom safe area boundary: one 10 points short of it has a bottom inset of 0 points, one on the boundary has 34 points, and one 20 points past it has 14 points

Views in the same container can have different insets, depending on which boundaries they reach. For example, in a VStack that divides the safe area between two flexible views, the first view reaches only the top boundary and the second only the bottom one, so the first view has an inset only on its top edge and the second only on its bottom edge.

VStack(spacing: 0) {
    IndigoRectangle()
    TealRectangle()
}
Two views stacked in the safe area of an iPhone window: the upper one has a top inset of 62 points and a bottom inset of 0, and the lower one has a top inset of 0 and a bottom inset of 34 points Two views stacked in the safe area of an iPhone window: the upper one has a top inset of 62 points and a bottom inset of 0, and the lower one has a top inset of 0 and a bottom inset of 34 points

The same principles apply to views created by layout modifiers. These views have their own layout bounds and can observe different safe area insets than the views they wrap. For example, the padding(_:_:) modifier creates a view whose bounds include the padding, with the modified view laid out inside it.

ZStack(alignment: .bottom) {
    Color.clear
    IndigoRectangle()
        .frame(height: 80)
        .padding()
}

In this example, the padding view is placed at the bottom of the safe area, so its frame reaches the boundary and its bottom inset is the distance to the window's bottom edge. The rectangle's frame lies short of the boundary, so its bottom inset is zero.

A padded view placed on the bottom safe area boundary: the padding view has a bottom inset of 34 points, and the view inside it has none A padded view placed on the bottom safe area boundary: the padding view has a bottom inset of 34 points, and the view inside it has none

Containers that display interface bars, such as NavigationStack, reduce the safe area of their content by the space their bars occupy.

NavigationStack {
    VStack(spacing: 0) {
        IndigoRectangle()
        TealRectangle()
    }
    .navigationTitle("Safe area")
}

The VStack is laid out within this reduced safe area, whose top boundary lies at the bottom edge of the navigation bar. The stack's first view reaches that boundary, so its top inset consists of the space occupied by the navigation bar and the status bar above it.

Two views stacked inside a navigation stack: the upper one starts below the navigation bar and has a top inset of 168 points that spans the status bar and the navigation bar, and the lower one has a bottom inset of 34 points Two views stacked inside a navigation stack: the upper one starts below the navigation bar and has a top inset of 168 points that spans the status bar and the navigation bar, and the lower one has a bottom inset of 34 points

A TabView adjusts the safe area of its content in a similar way to accommodate its tab bar. The software keyboard also affects the safe area, but contributes to the keyboard region, separate from the insets of device features and interface bars.

SwiftUI Fundamentals by Natalia Panferova book coverSwiftUI Fundamentals by Natalia Panferova book cover

Deepen your understanding of SwiftUI!$35

The essential guide to SwiftUI core concepts and APIs

SwiftUI Fundamentalsby Natalia Panferova

  • Explore the key APIs and design patterns that form the foundation of SwiftUI
  • Develop a deep, practical understanding of how SwiftUI works under the hood
  • Learn from a former Apple engineer who worked on widely used SwiftUI APIs

Deepen your understanding of SwiftUI!

The essential guide to SwiftUI core concepts and APIs

SwiftUI Fundamentals by Natalia Panferova book coverSwiftUI Fundamentals by Natalia Panferova book cover

SwiftUI Fundamentals

by Natalia Panferova

$35

# Adjusting the safe area with view modifiers

In addition to the adjustments made implicitly by containers and the system, SwiftUI provides a collection of view modifiers that let us change the safe area insets of a view explicitly.

# Ignoring the safe area insets

The ignoresSafeArea(_:edges:) modifier reduces the safe area insets of a view to zero on the specified edges, expanding its safe area by the same amount. The layout system then proposes the expanded size to the view.

VStack(spacing: 0) {
    IndigoRectangle()
        .ignoresSafeArea(edges: .top)
    TealRectangle()
}

The stack first divides the space available to it, which doesn't include the top inset, equally between its two subviews. The view created by ignoresSafeArea() is placed in the top half, so its top edge lies on the top boundary. The modifier then reduces the top inset of the rectangle inside it to zero, so the rectangle is proposed the top half expanded by that inset. As a flexible view, the rectangle accepts this size, extending to the top edge of the window.

The upper of two stacked views extends past the top safe area boundary to the top edge of the window, with a top inset of 0 points, and the lower view stays within the safe area The upper of two stacked views extends past the top safe area boundary to the top edge of the window, with a top inset of 0 points, and the lower view stays within the safe area

A view that has a fixed dimension along the axis of expansion behaves differently. It responds to the proposal with its own size, and by default, ignoresSafeArea() aligns it within the expanded bounds based on the edges being ignored.

VStack(spacing: 0) {
    IndigoRectangle()
        .frame(height: 300)
        .ignoresSafeArea(edges: .top)
    TealRectangle()
}

Since we're ignoring the top inset, the fixed-height rectangle is aligned to the top of the expanded bounds, leaving a gap the size of that inset between it and the second rectangle.

A fixed-height view aligned to the top edge of the window, with a gap the size of the top inset between it and the view below A fixed-height view aligned to the top edge of the window, with a gap the size of the top inset between it and the view below

If we need a different alignment, we can specify it with the ignoresSafeArea(_:edges:alignment:) modifier, available starting with iOS 27.

We can also limit the effect of ignoresSafeArea() to particular safe area regions. By default, the modifier ignores all regions, but specifying only the container region allows a view to extend into the insets associated with device features and interface bars while still avoiding the software keyboard.

VStack(spacing: 0) {
    NoteField()
    IndigoRectangle()
        .ignoresSafeArea(.container)
}

Without the keyboard, the rectangle's bottom inset belongs entirely to the container region, so the rectangle extends to the bottom edge of the window. When the keyboard is shown, the bottom safe area boundary moves up to accommodate it, and since we're not ignoring the keyboard region, the rectangle is laid out above the keyboard.

A note field at the top of the safe area above a rectangle: without the keyboard, the rectangle extends to the bottom edge of the window; with the keyboard shown, the bottom safe area boundary moves up to the top of the keyboard and the rectangle is laid out above it A note field at the top of the safe area above a rectangle: without the keyboard, the rectangle extends to the bottom edge of the window; with the keyboard shown, the bottom safe area boundary moves up to the top of the keyboard and the rectangle is laid out above it

A view's background can extend into the safe area insets without expanding the safe area of the view itself. When we use the background(_:ignoresSafeAreaEdges:) modifier with a shape style, SwiftUI automatically extends the background along the specified edges where the view reaches the safe area boundary. By default, this behavior is enabled for all edges.

IndigoRectangle()
    .background(.teal.gradient)

In this example, the rectangle fills the safe area, while its background extends through the top and bottom insets to the edges of the window.

A rectangle filling the safe area, with a top inset of 62 points and a bottom inset of 34 points, and its background extending into the insets to the top and bottom edges of the window A rectangle filling the safe area, with a top inset of 62 points and a bottom inset of 34 points, and its background extending into the insets to the top and bottom edges of the window

# Increasing the safe area insets

Increasing a view's safe area insets reduces its safe area, so the view is proposed a smaller size. To increase the insets by a fixed length on specific edges, we can use the safeAreaPadding(_:_:) modifier.

IndigoRectangle()
    .background(.teal.gradient)
    .safeAreaPadding(.bottom)

The rectangle's bottom inset increases by the padding length, and the rectangle accepts the smaller height it's proposed. Since its background ignores the safe area insets, the background extends through the entire bottom inset to the bottom edge of the window.

A rectangle laid out above the bottom safe area boundary, separated from it by a hatched strip that marks the padding length added to its bottom inset, giving a bottom inset of 50 points, with its background extending through the strip and the rest of the inset to the bottom edge of the window A rectangle laid out above the bottom safe area boundary, separated from it by a hatched strip that marks the padding length added to its bottom inset, giving a bottom inset of 50 points, with its background extending through the strip and the rest of the inset to the bottom edge of the window

Regular padding would give the rectangle the same size and position, but it doesn't add its length to the safe area insets. With padding(.bottom), the rectangle's bottom edge would lie short of the safe area boundary, so its bottom inset would be zero and its background would stop at that edge.

The SwiftUI Way by Natalia Panferova book coverThe SwiftUI Way by Natalia Panferova book cover

Work with SwiftUI. Not against it.$35

A field guide to SwiftUI patterns and anti-patterns

The SwiftUI Wayby Natalia Panferova

  • Avoid common SwiftUI pitfalls
  • Build deeper intuition for the framework
  • Gain insights from a former SwiftUI Engineer at Apple

Work with SwiftUI. Not against it.

A field guide to SwiftUI patterns and anti-patterns

The SwiftUI Way by Natalia Panferova book coverThe SwiftUI Way by Natalia Panferova book cover

The SwiftUI Way

by Natalia Panferova

$35

When the inset should match the size of another view rather than a fixed length, we can use the safeAreaInset(edge:alignment:spacing:content:) modifier, which increases the insets of the modified view by the height or width of the view returned from its closure, plus the spacing between them.

IndigoRectangle()
    .safeAreaInset(edge: .bottom) {
        TealRectangle()
            .frame(height: 50)
    }

Here, the modified view's bottom inset increases by 50 points plus the default spacing, and the view is laid out within its reduced safe area.

A rectangle laid out above the view returned from the closure, which is at the bottom of the safe area, with the default spacing between them; a dashed line marks the bottom boundary of the rectangle's reduced safe area, and hatching over the spacing and the view returned from the closure marks the length added to its bottom inset, giving a bottom inset of 92 points A rectangle laid out above the view returned from the closure, which is at the bottom of the safe area, with the default spacing between them; a dashed line marks the bottom boundary of the rectangle's reduced safe area, and hatching over the spacing and the view returned from the closure marks the length added to its bottom inset, giving a bottom inset of 92 points

# Safe area and content margins in scroll views

The content of a scroll view doesn't remain within the safe area the way other views generally do. The scroll view resolves its safe area insets into margins for its content, which lets the content scroll through the insets to the edges of the window.

ScrollView {
    VStack(spacing: 8) {
        ForEach(0..<20, id: \.self) { _ in
            IndigoRectangle()
                .frame(height: 60)
        }
    }
}

When the content is scrolled to the top, the first row starts at the top safe area boundary, and when it's scrolled to the bottom, the last row ends at the bottom boundary. The rows themselves have no safe area insets, even where they pass behind the status bar or the home indicator.

Rows in a scroll view: scrolled to the top, the first row starts at the top safe area boundary; scrolled to the bottom, the last row ends at the bottom safe area boundary Rows in a scroll view: scrolled to the top, the first row starts at the top safe area boundary; scrolled to the bottom, the last row ends at the bottom safe area boundary

Since the margins come from the scroll view's own safe area insets, they also include any increase made by view modifiers. When we apply safeAreaInset() to the scroll view, the increased bottom inset becomes part of its bottom margin.

ScrollView {
    VStack(spacing: 8) {
        ForEach(0..<20, id: \.self) { _ in
            IndigoRectangle()
                .frame(height: 60)
        }
    }
}
.safeAreaInset(edge: .bottom) {
    TealRectangle()
        .frame(height: 50)
}

The scroll view's frame ends above the view returned from the closure, but the rows still scroll behind that view to the bottom edge of the window. When the content is scrolled to the bottom, the last row ends above that view, separated by the default spacing.

Rows in a scroll view with safeAreaInset applied, with a dashed line marking the bottom boundary of the scroll view's safe area and hatching marking the length added to its bottom inset: scrolled midway, the rows continue behind the view returned from the closure to the bottom edge of the window; scrolled to the bottom, the last row ends at that boundary, above the view Rows in a scroll view with safeAreaInset applied, with a dashed line marking the bottom boundary of the scroll view's safe area and hatching marking the length added to its bottom inset: scrolled midway, the rows continue behind the view returned from the closure to the bottom edge of the window; scrolled to the bottom, the last row ends at that boundary, above the view

Horizontally, the content of a vertical scroll view is laid out within the safe area like other views, so increasing the scroll view's horizontal insets with safeAreaPadding(_:_:) moves the rows in from the edges of the window.

ScrollView {
    VStack(spacing: 8) {
        ForEach(0..<20, id: \.self) { _ in
            IndigoRectangle()
                .frame(height: 60)
                .background(.pink.gradient)
        }
    }
}
.safeAreaPadding(.horizontal, 24)

The leading and trailing edges of each row lie on the safe area boundaries, so each row has horizontal insets, and its background extends through them to the edges of the window. The scroll indicator is inset by the padding together with the content.

Rows in a scroll view with safeAreaPadding applied: hatched strips mark the leading and trailing insets added by the padding, the row backgrounds extend through them to the edges of the window, and the scroll indicator is inset by the padding Rows in a scroll view with safeAreaPadding applied: hatched strips mark the leading and trailing insets added by the padding, the row backgrounds extend through them to the edges of the window, and the scroll indicator is inset by the padding

To add margins to the content without changing the safe area insets, we can use the contentMargins(_:_:for:) modifier instead. Its placement parameter specifies where the margins are added, and the scrollContent placement adds them only to the content of the scroll view, not to its scroll indicators.

ScrollView {
    VStack(spacing: 8) {
        ForEach(0..<20, id: \.self) { _ in
            IndigoRectangle()
                .frame(height: 60)
                .background(.pink.gradient)
        }
    }
}
.contentMargins(.horizontal, 24, for: .scrollContent)

The rows are laid out at the same positions as before, but they have no insets, so their backgrounds stop at their edges, and the scroll indicator is drawn next to the trailing edge of the window.

Rows in a scroll view with contentMargins for scroll content: the rows are in the same positions, their backgrounds stop at the rows' edges, and the scroll indicator is next to the trailing edge of the window Rows in a scroll view with contentMargins for scroll content: the rows are in the same positions, their backgrounds stop at the rows' edges, and the scroll indicator is next to the trailing edge of the window

When adjusting the spacing around scrollable content, it's worth considering which of these approaches fits. Although both can result in the same size and position of the content, they differ in how far backgrounds and views that ignore the safe area extend, and in how the scroll view positions its indicators.


If you'd like to develop a deeper understanding of the SwiftUI layout system, my book SwiftUI Fundamentals explores the principles behind view sizing and positioning, while The SwiftUI Way focuses on recommended patterns for working with the framework. And if you use coding agents, the SwiftUI Layout & Resizability knowledge set in our Mac app SwiftFairy can help identify layout issues in the code your agent writes and provide guidance on addressing them.

For more resources on Swift and SwiftUI, check out my other books and book bundles.

SwiftFairy

Our Swift expertise, on call for your agent

On-device Swift and SwiftUI reviews with fix guidance

  • Proper SwiftUI
  • SwiftUI Layout & Resizability
  • Performant Swift Charts
  • Modern Swift