Skip to main content

How to force views to one side inside a stack using Spacer

About 3 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

How to force views to one side inside a stack using Spacer 관련

SwiftUI by Example

Back to Home

How to force views to one side inside a stack using Spacer | SwiftUI by Example

How to force views to one side inside a stack using Spacer

Updated for Xcode 15

SwiftUI centers its views by default, which means if you place three text views inside a VStack all three will sit centered vertically in the screen. If you want to change this – if you want to force views towards the top, bottom, left, or right of the screen – then you should use a Spacer view.

For example, this places one text view inside a VStack, which means it will be centered vertically:

VStack {
    Text("Hello World")
}

Download this as an Xcode projectopen in new window

A phone with the text “Hello World” in the center of the screen.
A phone with the text “Hello World” in the center of the screen.

To push that text view to the top of the parent, we'd place a spacer below it, like this:

VStack {
    Text("Hello World")
    Spacer()
}

Download this as an Xcode projectopen in new window

A phone with the text “Hello World” at the top of the screen.
A phone with the text “Hello World” at the top of the screen.

If you wanted two pieces of text on the leading and trailing edges of a HStack, you would use a spacer like this:

HStack {
    Text("Hello")
    Spacer()
    Text("World")
}

Download this as an Xcode projectopen in new window

The words “Hello” and “World” at opposite ends of the image.
The words “Hello” and “World” at opposite ends of the image.

Spacers automatically divide up all remaining space, which means if you use if you use several spacers you can divide up the space in varying amounts.

For example, this will place a text view one third of the way down its parent view by putting one spacer before it and two after:

VStack {
    Spacer()
    Text("Hello World")
    Spacer()
    Spacer()
}

Download this as an Xcode projectopen in new window

A phone with the text “Hello World” two thirds of the way up the screen.
A phone with the text “Hello World” two thirds of the way up the screen.
Similar solutions…
How to show text and an icon side by side using Label | SwiftUI by Example

How to show text and an icon side by side using Label
How to make a fixed size Spacer | SwiftUI by Example

How to make a fixed size Spacer
How to force one gesture to recognize before another using highPriorityGesture() | SwiftUI by Example

How to force one gesture to recognize before another using highPriorityGesture()
How to customize stack layouts with alignment and spacing | SwiftUI by Example

How to customize stack layouts with alignment and spacing
How to stack modifiers to create more advanced effects | SwiftUI by Example

How to stack modifiers to create more advanced effects

이찬희 (MarkiiimarK)
Never Stop Learning.