SwiftUI Spacer
Published Jun 23, 2023
Contribute to Docs
A Spacer view is an adaptive view that creates space between views. It expands in dimensions as much as the parent view will allow.
Syntax
Spacer()
Examples
In the example below, the Spacer() in the HStack fills the view to the left of the Text, effectively pushing the Text to the right.
var body: some View {HStack {Spacer()Text("Example")}}
This will display the following:

In the next example below, each Spacer() fills the view on both the left and right of the Text. This effectively pushes the Text into the center, with each Spacer expanding as much as the parent view will allow on each side.
var body: some View {HStack {Spacer()Text("Example")Spacer()}}
This will display the following:

Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.