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.
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()
}
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.
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.
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.
# 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.
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.
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 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.
# 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.
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.
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.
# 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.
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.
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.
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.
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.
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





