Build: Stepper Demo
This screen shows a stepper control that lets the user increase or decrease a number. The current number is displayed in the center.
Target UI
--------------------- | | | 0 | | | | [-] [+] | | | ---------------------
--------------------- | | | 0 | | | | [-] [+] | | | ---------------------
import UIKit class StepperViewController: UIViewController { let numberLabel = UILabel() let stepper = UIStepper() override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .white numberLabel.text = "0" numberLabel.font = UIFont.systemFont(ofSize: 48) numberLabel.textAlignment = .center numberLabel.translatesAutoresizingMaskIntoConstraints = false view.addSubview(numberLabel) stepper.translatesAutoresizingMaskIntoConstraints = false view.addSubview(stepper) // TODO: Add target action for stepper value change // TODO: Add constraints for numberLabel and stepper } // TODO: Implement method to update numberLabel when stepper changes }
import UIKit class StepperViewController: UIViewController { let numberLabel = UILabel() let stepper = UIStepper() override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .white numberLabel.text = "0" numberLabel.font = UIFont.systemFont(ofSize: 48) numberLabel.textAlignment = .center numberLabel.translatesAutoresizingMaskIntoConstraints = false view.addSubview(numberLabel) stepper.minimumValue = 0 stepper.maximumValue = 100 stepper.stepValue = 1 stepper.value = 0 stepper.translatesAutoresizingMaskIntoConstraints = false stepper.addTarget(self, action: #selector(stepperValueChanged), for: .valueChanged) view.addSubview(stepper) NSLayoutConstraint.activate([ numberLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor), numberLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: -40), stepper.centerXAnchor.constraint(equalTo: view.centerXAnchor), stepper.topAnchor.constraint(equalTo: numberLabel.bottomAnchor, constant: 20) ]) } @objc func stepperValueChanged(_ sender: UIStepper) { numberLabel.text = String(Int(sender.value)) } }
We set up a UILabel to show the number and a UIStepper below it. The stepper's minimum is 0 and maximum is 100, stepping by 1. We add a target action for the stepper's value change event. When the user taps plus or minus, the stepperValueChanged method updates the label text to the current stepper value. Auto Layout centers the label and places the stepper below it with spacing. This creates a simple interactive stepper screen.
--------------------- | | | 5 | | | | [-] [+] | | | ---------------------
Stepper in iOS Swift UI?count?value: Binding and in: ClosedRange parameters.Stepper(value: $count, in: 0...10). Others have wrong parameter names or missing bindings.@State private var count = 3
Stepper(value: $count, in: 1...5) { Text("Count: \(count)") }@State private var count = 0
Stepper(value: count, in: 0...10) { Text("Count: \(count)") }$count, not just count.step: parameter. The range uses in:.value: $count, in: 10...50, and step: 5. Stepper(value: $count, range: 10...50, step: 5) { Text("Count: \(count)") } uses wrong parameter range. Stepper(value: $count, in: 10...50) { Text("Count: \(count)") } misses step size. Stepper(value: $count, in: 10...50, step: 10) { Text("Count: \(count)") } uses wrong step size 10.Pass the test and this pattern moves unknown