How it works
ScenePadding is a structure that describes an amount of padding measured against a scene's safe areas rather than a fixed number of points. You pass it to the scenePadding(_:edges:) modifier when you want a view's spacing to match the system-defined metrics for the current scene, so content lines up with the platform's conventions instead of an arbitrary constant. Reach for it whenever a hard-coded inset would look right on one device but wrong on another, and you'd rather defer the exact measurement to SwiftUI.
Apply padding with scenePadding(_:edges:)
The
scenePadding(_:edges:)modifier wraps a view and insets it by a scene-relative amount on the chosen edges, deriving the distance from the scene's recommended metrics. In the example it's attached to theVStack, padding it relative to the surrounding scene before any other layout runs.Choose the amount with a ScenePadding value
The first argument is a
ScenePaddingvalue that selects which system metric to use rather than a literal point count. Here.minimumrequests the smallest scene-appropriate inset, letting SwiftUI pick the concrete number for the active platform.Limit the effect with the edges parameter
The
edges:parameter takes anEdge.Setthat restricts where the scene padding is applied, so you can pad only the sides that matter. The example passes.horizontal, confining the scene-relative inset to the leading and trailing edges and leaving top and bottom untouched.Compose with ordinary padding()
scenePaddingcoexists with conventional spacing modifiers, so you can layer a fixed inset on top of the scene-relative one. Following the.scenePadding(.minimum, edges: .horizontal)call with.padding()adds standard padding around the already scene-padded content.
edges: .horizontal to edges: .all in the scenePadding(.minimum, edges:) call to see the scene-relative inset extend to the top and bottom edges as well.Example & preview
Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.
struct ScenePaddingDemo: View {
var body: some View {
VStack(alignment: .leading, spacing: 8) {
Text("Welcome Back")
.font(.headline)
Text("Your scene-padded content respects platform metrics.")
.foregroundStyle(.secondary)
}
.scenePadding(.minimum, edges: .horizontal)
.padding()
}
}