In SwiftUI, how do ignoresSafeArea and safeAreaInset differ, and which keeps a custom bottom bar clear of the home indicator?
answer
- extend into it, or shrink it
- content in, background out
- container and keyboard regions
- the scroll view learns about the bar
basics
~20 sIn SwiftUI, ignoresSafeArea extends a view into the safe area, while safeAreaInset(edge:) places a view at an edge and insets the modified view to make room. Put a bottom bar in safeAreaInset; let only its background ignore the safe area.
solid answer
~50 sBy default SwiftUI sizes and positions views to avoid the safe areas, so system features like the home indicator or the keyboard do not cover them. `ignoresSafeArea(_:edges:)` (iOS 14) opts a view out: SwiftUI proposes the expanded size, by default for every region (`.container` and `.keyboard`) on every edge. `safeAreaInset(edge:alignment:spacing:content:)` (iOS 15) does the opposite job: it places your content at an edge and insets the modified view by the content's size, growing that view's safe area. For a bottom buy bar, attach `.safeAreaInset(edge: .bottom)` to the `ScrollView`: the bar stays above the home indicator and the scroll content can scroll its last rows clear of it. Give the bar a `background(.bar)`; `background(_:ignoresSafeAreaEdges:)` defaults to all edges, so the fill reaches the screen edge while the buttons stay inside. Applying `ignoresSafeArea()` to the whole screen instead pushes the bar under the home indicator.
code
swift · 25 linesimport SwiftUI
struct ProductDetailScreen: View {
let name: String
let price: Decimal
var body: some View {
ScrollView {
Text(name)
.font(.largeTitle)
.frame(maxWidth: .infinity, alignment: .leading)
.padding()
}
.safeAreaInset(edge: .bottom, spacing: 0) {
HStack {
Text(price, format: .currency(code: "EUR"))
Spacer()
Button("Add to Bag") {}
.buttonStyle(.borderedProminent)
}
.padding()
.background(.bar)
}
}
}go deeper
Recall that SwiftUI keeps views inside the safe area by default, ignoresSafeArea extends a view out of it, and safeAreaInset adds a bar that the content makes room for.
Explain the defaults: ignoresSafeArea covers both the container and keyboard regions on all edges, and background(_:ignoresSafeAreaEdges:) already extends its fill to every edge.
Diagnose the bar-over-scroll-view bug from symptoms: hidden last rows mean the scroll view was not inset; buttons on the home indicator mean ignoresSafeArea was applied too broadly.
Standardise custom bars as a shared component built on safeAreaInset, so every screen gets correct scroll insets, keyboard behaviour and edge-to-edge material without per-screen tweaks.
## What the safe area is The **safe area** is the part of a view that system content does not obscure: the status bar and sensor housing at the top, the home indicator at the bottom, navigation and tab bars, and the software keyboard when it is up. Apple's documentation says that, by default, the SwiftUI layout system sizes and positions views to avoid these areas. SwiftUI models them as `SafeAreaRegions`, a set with `.container` (the device and containers in the interface, including top and bottom bars), `.keyboard` and `.all`. ## Two modifiers, opposite jobs | | `ignoresSafeArea(_:edges:)` | `safeAreaInset(edge:alignment:spacing:content:)` | |---|---|---| | Available from | iOS 14 | iOS 15 | | Effect on the view | expands it into the safe area | insets it to make room for new content | | Effect on the safe area | the view no longer avoids it | the modified view's safe area grows by the content's size | | Defaults | regions `.all`, edges `.all` | `spacing: nil` uses the default spacing | | Typical use | full-bleed backgrounds and photos | custom bottom bars, side rails, banners | `ignoresSafeArea` replaces the older `edgesIgnoringSafeArea(_:)`, which is marked for deprecation with the message "Use ignoresSafeArea(_:edges:) instead." When it expands a view, Apple notes that the layout system proposes the expanded size, so a fixed-size view may end up aligned differently than you expect. ## The product screen bug A common first attempt draws a buy bar over a scroll view: ```swift ZStack(alignment: .bottom) { ScrollView { ProductDetails() } BuyBar() } .ignoresSafeArea() ``` Two things go wrong: 1. `ignoresSafeArea()` applies to the whole stack, so `BuyBar` is laid out into the bottom safe area and its buttons collide with the home indicator. 2. The scroll view has no idea the bar exists, so its last rows scroll underneath the bar and can never be fully revealed. ## The fix ```swift ScrollView { ProductDetails() } .safeAreaInset(edge: .bottom, spacing: 0) { BuyBar() .padding() .background(.bar) } ``` - `safeAreaInset` places `BuyBar` at the bottom edge and insets the scroll view by the bar's height, so the scroll view's safe area now ends at the top of the bar and the final rows can scroll into view above it. - The bar's own content respects the safe area, so its buttons sit above the home indicator. - `.background(.bar)` uses `background(_:ignoresSafeAreaEdges:)`, whose `edges` default is `.all`, so the material extends down to the screen edge behind the indicator while the content stays put. ## Fixed-size views and ignored edges When `ignoresSafeArea` expands a view, the layout system proposes the expanded size to it. A view that does not take the whole offer, such as one with a fixed frame, is then placed inside the larger area, and Apple warns that its alignment may not be what you expect. By default SwiftUI resolves that alignment from the edges being ignored: ignoring only the bottom edge, for example, gives a bottom alignment. This is one more reason to apply the modifier to a flexible background rather than to content with a fixed size. ## Narrower tools - **Background only:** Apple's own example applies `.ignoresSafeArea()` to the background gradient inside a `ZStack`, not to the stack, so the gradient is full-bleed and the form stays inside the safe area. - **One region:** `.ignoresSafeArea(.keyboard)` keeps a layout fixed when the keyboard appears, while still respecting the container edges. - **One edge:** `.ignoresSafeArea(edges: .bottom)` extends only downwards. - **Fixed padding:** `safeAreaPadding(_:_:)` (iOS 17) adds a fixed amount to the safe area, useful for scroll margins; use `safeAreaInset` when the space should match a real view's size. ## What interviewers check - Whether you apply `ignoresSafeArea` to the narrowest view that needs it, usually a background. - Whether the scroll content accounts for overlaid bars, which `safeAreaInset` does and a `ZStack` overlay does not. - Whether you remember the keyboard is a safe-area region too, so ignoring `.all` also stops content moving out of the keyboard's way. - Whether you know that the container region already includes system top and bottom bars; a custom bar joins them through `safeAreaInset`.
- What happens to a form when you apply .ignoresSafeArea() to the whole screen and the keyboard appears?The default regions are `.all`, which includes `.keyboard`, so the form ignores the keyboard's safe area and stays where it is; fields near the bottom can end up behind the keyboard. Apply `ignoresSafeArea()` only to the background, or limit it to `.container`, so the content still moves out of the keyboard's way.
- When would you choose safeAreaPadding over safeAreaInset?`safeAreaPadding(_:_:)`, available from iOS 17, adds a fixed amount to the safe area, for example horizontal margins on a scroll view so the first and last items are not flush with the screen. `safeAreaInset` adds space equal to the size of real content you supply, which is what a bar or banner needs.
- ignoresSafeArea on the whole screen is the standard way to add a bottom bar.
- safeAreaInset just overlays a view and does not change the content's layout.
- The keyboard is not part of the safe area, so ignoresSafeArea() never affects it.
- A ZStack overlay lets the ScrollView know how tall the bottom bar is.
- background(.bar) stops at the safe area unless you add ignoresSafeArea to it.