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.
Choose a value: .enabled or .disabled
ButtonRepeatBehavioris a struct that exposes named constants describing the two repeat modes. Use.enabledto opt a button into auto-repeat so its action runs again and again while held, or.disabledto force the single-fire behavior even where an enclosing context would otherwise allow repeating. The example passes.enabledto turn on press-and-hold repeating.Apply it with buttonRepeatBehavior(_:)
The behavior reaches the button through the
buttonRepeatBehavior(_:)view modifier, which takes aButtonRepeatBehaviorvalue and writes it into the environment for the buttons it wraps. In the example it's attached directly to theButton, as.buttonRepeatBehavior(.enabled), so this particular button repeats; placed higher in the hierarchy it would govern every button beneath it.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 iscount += 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.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 sameButton.
.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.
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()
}
}