Member-only story

The debut of the ButtonRepeatBehavior view modifier in SwiftUI

SwiftUI makes it finally natively possible to repeat a button’s action on long press.

Gabriel Theodoropoulos
4 min readJun 20, 2023
Photo by Laura Tancredi: https://www.pexels.com/photo/background-of-bright-red-bricks-7078227/

As it happens every year in WWDC, Apple came to announce new development tools and bring advancements to existing ones. Some of them are big, some others are more subtle and not that apparent until they get discovered.

Such a small but quite interesting and useful addition is a brand new view modifier in SwiftUI called ButtonRepeatBehavior. Its purpose is pretty straightforward, and that is to let users repeat a button’s action repeatedly while pressing and holding it.

Let’s get to know it.

So far there was just the Stepper

Before WWDC 2023, the only control that allowed us to increase or decrease a value repeatedly by long pressing on any of its buttons was the Stepper; a horizontal control that consists of two buttons and a label where we can show the current value. Stepper is ideal in order to adjust numerical values that should change incrementally.

For example, we can create a Stepper like this:

struct ContentView: View {
@State private var volume = 50

var body: some View {
Stepper(“Volume…

--

--

Gabriel Theodoropoulos
Gabriel Theodoropoulos

Written by Gabriel Theodoropoulos

An iOS & macOS app maker writing code in Swift. Author of countless programming tutorials. Content creator. https://serialcoder.dev

No responses yet