disabled

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

View ModifieriOS 13.0+macOS 10.15+tvOS 13.0+watchOS 6.0+disabled(_:) in SwiftUI

Declaration

React
compose().disabled(disabled: boolean)
Swift
nonisolated func disabled(_ disabled: Bool) -> some View

Parameters

  • disabled*boolean

    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

More in Input and interaction.