跳转至
Structure

Spacer

A flexible empty view that expands to take up as much space as possible.

Declaration

@frozen struct Spacer

Overview

Spacers are commonly used inside of stacks, like VStack, HStack, and ZStack. In a stack, they take up as much space as possible, so push everything else to the side.

In the following HStack, a spacer is used to align the text to the right:

struct SpacerView: View {
    var body: some View {
        HStack {
            Spacer()
            Text("Ouch I'm crammed ➡️🤕")
        }
    }
}
A screenshot displaying text reading "Ouch I'm crammed" which is pushed up against the right side of the screen. Arranging the text in an HStack next to a spacer places the text as far right as possible.

A spacer can also be framed to take a specific amount of space:

struct SpacerView: View {
    var body: some View {
        VStack {
            Spacer()
            Text("I'm 15 points off the ground 😇")
            Spacer()
                .frame(height: 15)
        }
    }
}
A screenshot displaying text at the bottom of the screen that reads "I'm 15 points off the ground." The text is arranged in a VStack sandwiched by a spacer on either side, but the bottom spacer is framed to only take up 15 points.

Availability

iOS 13.0+

macOS 10.15+

tvOS 13.0+

watchOS 6.0+

Topics


Instance Property

minLength The minimum length the spacer will take up.


Initializer

init(minLength:) Creates a spacer view.


Type Alias

Body The type of view representing the body of this view.