Spacer

josephlr's avatar
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:

SwiftUI Spacer Example 1

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:

SwiftUI Spacer Example 2

All contributors

Contribute to Docs