A control that performs an action when triggered.
- iOS 13.0+
- macOS 10.15+
- Mac Catalyst 13.0+
- tvOS 13.0+
- watchOS 6.0+
- Xcode 11.0+
You create a button by providing an action and a label. The action is either a method or closure property that does something when a user clicks or taps the button. The label is a view that describes the button’s action, for example, by showing text such as Cancel or an icon such as a back arrow.
For the common case of text-only labels, you can use the convenience initializer that takes a title string (or localized string key) as its first parameter, instead of a trailing closure:
The method of triggering the button varies by platform:
In iOS and watchOS, the user triggers a standard button by tapping on it.
In macOS, the user triggers a standard button by clicking on it.
In tvOS, the user triggers a standard button by pressing “select” on an external remote, like the Siri Remote, while focusing on the button.
Adding Buttons to Containers
Use buttons for any user interface element that triggers actions on press. Buttons automatically adapt their visual style to match the expected style within these different containers and contexts. For example, to create a list cell that triggers an action when selected by the user, add a button to the list’s content. For example:
Similarly, to create a context menu item that triggers an action, add a button to the menu’s content:
This pattern extends to most other container views in SwiftUI that have customizable, interactive content, like forms (instances of
You can customize a button’s appearance and interaction behavior. To add a custom appearance with standard interaction behavior, create a style that conforms to the
Button protocol. To customize both appearance and interaction behavior, create a style that conforms to the
Primitive protocol. To set a specific style for all button instances within a view, use the