Here is an example of how to make a simple stepper in SwiftUI:
import SwiftUI
struct StepperExample: View {
@State private var value: Int = 0
var body: some View {
VStack {
Text("Value: \(value)")
HStack {
Button("-") {
self.value -= 1
}
.padding()
Button("+") {
self.value += 1
}
.padding()
}
}
}
}
In this example, a VStack contains a Text element that displays the current value of the stepper, and an HStack that contains two Button elements for incrementing and decrementing the value. The value property is a @State property, which allows the view to re-render when the value changes.
You can use Stepper too from the SwiftUI framework to make it more simpler to create stepper.
import SwiftUI
struct StepperExample: View {
@State private var value: Int = 0
var body: some View {
VStack {
Text("Value: \(value)")
Stepper("Step Value", value: $value)
}
}
}
The Stepper("Step Value", value: $value) creates the stepper and the value state is binded via the $value this allows the text Step Value to change depending on the current value.
Tags:
SwiftUI