< How to create a segmented control and read values from it | How to read tap and double-tap gestures > |
Updated for Xcode 12.0
SwiftUI’s Stepper
control lets users select values from a range we specify, providing the same functionality as UIStepper
from UIKit.
As an example, this creates a stepper bound to an age
property, letting users select values in the range 0 through 130 inclusive:
struct ContentView: View {
@State private var age = 18
var body: some View {
VStack {
Stepper("Enter your age", value: $age, in: 0...130)
Text("Your age is \(age)")
}
}
}
Rather than binding directly to a value, you can also provide custom onIncrement
and onDecrement
closures to do custom work, like this:
struct ContentView: View {
@State private var age = 18
var body: some View {
VStack {
Stepper("Enter your age", onIncrement: {
self.age += 1
print("Adding to age")
}, onDecrement: {
self.age -= 1
print("Subtracting from age")
})
Text("Your age is \(age)")
}
}
}
SPONSORED Would you describe yourself as knowledgeable, but struggling when you have to come up with your own code? Fernando Olivares has a new book containing iOS rules you can immediately apply to your coding habits to see dramatic improvements, while also teaching applied programming fundamentals seen in refactored code from published apps.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.