Button

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 = 0
var 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:

Button

All contributors

Looking to contribute?

Learn SwiftUI on Codecademy