TechnologiesSwiftUIControls and Style Configurations

ButtonRepeatBehavior struct

iOSmacOStvOSwatchOSvisionOSiOS 17.0+✓ renders

The options for controlling the repeatability of button actions.

How it works

ButtonRepeatBehavior is a small set of constants that tells SwiftUI whether a button should fire its action repeatedly while the user holds it down, rather than only once on release. By default a button triggers a single action per press, which is the right behavior for most controls; but for actions that are naturally incremental — adjusting a counter, scrubbing a value, fast-forwarding — you want the action to keep firing as long as the press continues. Reach for ButtonRepeatBehavior when you want that press-and-hold auto-repeat, applying it through the buttonRepeatBehavior(_:) modifier so SwiftUI knows to re-invoke the action on a recurring cadence.

  1. Choose a value: .enabled or .disabled

    ButtonRepeatBehavior is a struct that exposes named constants describing the two repeat modes. Use .enabled to opt a button into auto-repeat so its action runs again and again while held, or .disabled to force the single-fire behavior even where an enclosing context would otherwise allow repeating. The example passes .enabled to turn on press-and-hold repeating.

  2. Apply it with buttonRepeatBehavior(_:)

    The behavior reaches the button through the buttonRepeatBehavior(_:) view modifier, which takes a ButtonRepeatBehavior value and writes it into the environment for the buttons it wraps. In the example it's attached directly to the Button, as .buttonRepeatBehavior(.enabled), so this particular button repeats; placed higher in the hierarchy it would govern every button beneath it.

  3. Let the action carry the repeated effect

    Because the action closure is what SwiftUI re-invokes, design it to do meaningful work each time it runs. Here the Button("Hold to Increment") action is count += 1, so each repeat tick advances the @State private var count — holding the button drives the count upward continuously instead of one tap at a time.

  4. Combine freely with button styling

    buttonRepeatBehavior(_:) only affects when the action fires; it doesn't touch appearance, so it composes with the usual style modifiers. The example chains .buttonStyle(.borderedProminent) right after it, and the two are independent — the repeat behavior and the visual style each configure their own aspect of the same Button.

Try it — Change .buttonRepeatBehavior(.enabled) to .buttonRepeatBehavior(.disabled) and notice the count advances only once per press no matter how long you hold the button.

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.

ButtonRepeatBehavior.swift
struct ButtonRepeatBehaviorDemo: View {
    @State private var count = 0
    var body: some View {
        VStack(spacing: 16) {
            Text("Count: \(count)")
                .font(.title2)
            Button("Hold to Increment") {
                count += 1
            }
            .buttonRepeatBehavior(.enabled)
            .buttonStyle(.borderedProminent)
        }
        .padding()
    }
}
Live preview
Count: 0 Hold to Increment
Count: 0 Hold to Increment
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →