TechnologiesSwiftUI

Button struct

iOSmacOStvOSwatchOSvisionOSiOS 13.0+✓ renders

A control that initiates an action.

How it works

A Button is a control that performs an action when the person interacting with your interface triggers it. You give it a label that describes the action and a closure that runs when it's activated, and SwiftUI takes care of the gesture handling, accessibility, and platform-appropriate appearance. Reach for Button whenever a tap, click, or press should make something happen — incrementing a value, submitting a form, navigating, or resetting state.

  1. Create a button with a title and an action

    The most concise initializer takes a title string and a trailing action closure, producing a button whose label is a Text view. In the example, Button("Tap me") { count += 1 } shows the title and runs the closure each time it's activated, mutating the @State value count.

  2. Provide a custom label with the label: closure

    When a plain string isn't enough, use the initializer that pairs an action closure with a label view builder, letting the label be any view. The example builds label: { Label("Reset", systemImage: "trash") } so the button presents text alongside an SF Symbol image instead of bare text.

  3. Signal intent with the role: parameter

    A ButtonRole tells SwiftUI the semantic purpose of the action so it can style the button and adjust accessibility accordingly. Passing role: .destructive marks the reset button as one that removes or undoes data, which the system can render with a cautionary appearance and announce appropriately.

  4. Shape the appearance with buttonStyle(_:)

    The buttonStyle(_:) modifier applies a ButtonStyle to a button and to any buttons nested within that view hierarchy, separating how a button looks from what it does. Here .buttonStyle(.borderedProminent) gives the first button a filled, emphasized treatment; other built-in styles include .bordered, .borderless, and .plain.

Try it — Change .buttonStyle(.borderedProminent) to .buttonStyle(.bordered) to see how the same Button adopts a lighter, less emphasized appearance without touching its action.

Example & preview

Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.

Button.swift
struct ButtonDemo: View {
    @State private var count = 0
    var body: some View {
        VStack(spacing: 16) {
            Text("Taps: \(count)")
                .font(.headline)
            Button("Tap me") {
                count += 1
            }
            .buttonStyle(.borderedProminent)
            Button(role: .destructive) {
                count = 0
            } label: {
                Label("Reset", systemImage: "trash")
            }
        }
        .padding()
    }
}
Live preview
Taps: 0 Tap me Delete
Taps: 0 Tap me Delete
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →