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.