Button
Published Oct 17, 2022Updated Oct 17, 2022
Contribute to Docs
A button is an interactive component in the user interface (UI) that allows users to perform certain actions, such as logging into a site or deleting a file.
Syntax
Button("Label of button") {
// What happens when the button is used
}
The "Label of button"
is what is shown on the button. It can be an icon, text or a combination of both.
Example
The following example increases the count
each time the Button
is used:
@State var count = 0var body: some View {Button("Click me: \(count)") {self.count += 1}.font(.title).padding().backgroundColor(Color.green).cornerRadius(40).foregroundColor(Color.black).overlay(RoundedRectangle(cornerRadius: 40).stroke(Color.green))
Each time the user selects the Button
, the number next to the text "Click Me"
will increase self.count
by 1.
This will display the following:
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.