Contents

disabled(_:)

Adds a condition that controls whether someone can interact with this view.

Declaration

nonisolated func disabled(_ disabled: Bool) -> some View

Parameters

  • disabled:

    A Boolean value that determines whether someone can interact with this view.

Return Value

A view that controls whether someone can interact with this view.

Discussion

Add this modifier to a view to indicate whether it responds to interactions such as a tap, click, or gesture. The following example disables a Button when the Boolean value isLoading is true:

@State private var isLoading: Bool = false

var body: some View {
    Button("Load") { }
        .disabled(isLoading)
}

Add this modifier to a container, such as a VStack, to indicate whether views in the container are currently interactive, and to override any .disabled(_:) modifiers you set on views within the container or view hierarchy. In the following example, the .disabled(true) modifier on the VStack disables all of the Button instances in the stack, even the button that has the modifier .disabled(false).

VStack {
    Button("One") {}
        .disabled(false)
    Button("Two") {}
    Button("Three") {}
}
.disabled(true)

See Also

Managing view interaction