SwiftUI is a modern user interface (UI) framework from Apple that makes creating user interfaces for iOS, macOS, watchOS, and tvOS apps easier than ever before. It provides a declarative syntax that allows developers to quickly create beautiful and powerful user interfaces. One of the most useful components of SwiftUI is the stepper. Steppers are used to allow users to increment or decrement a value, such as a counter or a text field. In this article, we will explore how to use steppers in iOS development with Swift.
What is a Stepper?
A stepper is an iOS control that allows users to increment or decrement a value in small increments. Steppers are commonly used for selecting a number of items, such as the number of tickets to purchase or the number of people in a group. Steppers are also used to select a range of values, such as a temperature range or a time range. Steppers are composed of two buttons, one to increment the value and one to decrement the value.
Creating a Stepper in SwiftUI
Creating a stepper in SwiftUI is straightforward. Because SwiftUI is declarative, a Stepper is a view that you bind to a state property rather than an object whose properties you mutate directly. The snippet below declares a quantity state value and binds a Stepper to it with a range of 0 to 10 and an initial value of 5.
import SwiftUIGet iOS Guru’s stories in your inbox
Join Medium for free to get updates from this writer.
struct ContentView: View {
@State private var quantity = 5
var body: some View {
Stepper("Quantity: \(quantity)", value: $quantity, in: 0...10)
.padding()
}
}
A Stepper can be embedded in any container view alongside other content. The snippet below places it inside a VStack with a Text that reflects the current value, and uses the trailing closure form to provide the stepper's accessibility label.
struct StepperDemo: View {
@State private var quantity = 5
var body: some View {
VStack(spacing: 16) {
Text("Value: \(quantity)")
.font(.headline)
Stepper(value: $quantity, in: 0...10, step: 1) {
Text("Adjust quantity")
}
}
.padding()
}
}When you need finer control over what happens on each tap, Stepper exposes onIncrement and onDecrement closures so you can run custom logic instead of mutating a bound value directly. You can also style the control with standard SwiftUI modifiers such as .tint. The snippet below combines both.
struct StepperDemo: View {
@State private var quantity = 5
var body: some View {
Stepper(
onIncrement: { quantity += 1 },
onDecrement: { quantity = max(0, quantity - 1) }
) {
Text("Quantity: \(quantity)")
}
.tint(.accentColor)
.padding()
}
}Conclusion
Steppers are an essential component of user interfaces in iOS development with Swift. They provide a simple and intuitive way for users to increment or decrement a value. With SwiftUI, creating and customizing steppers is easy and straightforward. In this article, we have explored how to create and customize steppers in SwiftUI.










