/

SwiftUI forms: Stepper

SwiftUI forms: Stepper

In this blog post, we are going to explore the Stepper view, which is a helpful control in SwiftUI forms. The Stepper allows us to select a number and provides a - and + button to decrease or increase it.

To associate the Stepper with a property, we need to use the @State property wrapper. In this example, we have a property called counter that will be linked to the Stepper:

1
2
3
4
5
6
7
8
9
struct ContentView: View {
@State private var counter = 0

var body: some View {
Form {
Stepper("The counter is \(counter)", value: $counter)
}
}
}

Stepper Example

To limit the range of values that the Stepper can accept, we can use the in parameter:

1
Stepper("The counter is \(counter)", value: $counter, in: 0...10)

When the counter reaches the specified limit, the buttons to increase or decrease the value will become gray and non-interactive.

Tags: SwiftUI, forms, Stepper, iOS development