Button Repeat Behavior in SwiftUI

  • Feb 5, 2026

Button Repeat Behavior in SwiftUI

Starting in iOS 17, SwiftUI introduces a new modifier called buttonRepeatBehavior(_:), which enables buttons to repeat their action automatically while being pressed and held.

This modifier is especially useful for increment/decrement controls, steppers, counters, volume controls, timers, or any interaction where continuous input is more natural than repeated taps.

Let’s look at this with a simple example. We will create a simple view

struct ContentView: View {
    @State private var counter = 0
    var body: some View {
        VStack(spacing: 20) {
            Text("I like DevTechie.com **\(counter)** times.")
            Image(systemName: "heart.fill")
                .font(.largeTitle)
                .symbolEffect(.bounce, value: counter)
                .foregroundStyle(.red.gradient)
            HStack {
                Button("+") {
                    counter += 1
                }
                Button("-") {
                    counter += 1
                }
                
            }
            .buttonStyle(.glassProminent)
        }
    }
}

In the current implementation, the user must tap the buttons repeatedly to increment or decrement the counter. This interaction is not ideal for rapid value changes and results in unnecessary friction.

This is a perfect use case for the new buttonRepeatBehavior(_:) modifier. By enabling repeat behavior, the button’s action is automatically invoked continuously while the user presses and holds it, providing a much more natural and responsive experience.

Since both the increment and decrement buttons benefit from this behavior, we can apply the modifier at the HStack level. Doing so allows all buttons within the stack to inherit the repeat behavior.

struct ContentView: View {
    @State private var counter = 0
    var body: some View {
        VStack(spacing: 20) {
            Text("I like DevTechie.com **\(counter)** times.")
            Image(systemName: "heart.fill")
                .font(.largeTitle)
                .symbolEffect(.bounce, value: counter)
                .foregroundStyle(.red.gradient)
            HStack {
                Button("+") {
                    counter += 1
                }
                Button("-") {
                    counter += 1
                }
                
            }
            .buttonStyle(.glassProminent)
            .buttonRepeatBehavior(.enabled)
        }
    }
}

🔥 Learning about Apple Foundation Models? 

Take a deep dive with our book Apple Foundation Models with SwiftUI: Next-Gen Machine Learning and Generative AI Apps. This guide walks you step-by-step through building modern AI-powered applications using Apple’s latest foundation models and SwiftUI — from fundamentals to advanced real-world use cases.

Apple Foundations Model with SwiftUI: Next-Gen Machine Learning and Generative AI Apps
Learn how to build next-generation machine learning and generative AI apps using Apple Foundation Models and SwiftUI…www.devtechie.com

Check out more at https://www.devtechie.com