UIStepper
UIStepper
is a UIControl
subclass in UIKit that provides a user interface for incrementing or decrementing a numeric value. It features two buttons (plus and minus) for adjusting the value and is commonly used in iOS apps for tasks like selecting quantities, adjusting settings, or setting numerical preferences.
Properties
Property | Type | Description | Default Value |
---|---|---|---|
value | Double | The current numeric value of the stepper. | 0.0 |
minimumValue | Double | The lowest possible value. | 0.0 |
maximumValue | Double | The highest possible value. | 100.0 |
stepValue | Double | The increment/decrement amount per tap. | 1.0 |
isContinuous | Bool | If true , sends continuous updates while a button is held. | true |
autorepeat | true | If true , holding a button continuously increments/decrements the value. | Always true |
wraps | Bool | If true , the value wraps around when it exceeds the minimum/maximum. | false |
tintColor | UIColor? | The color of the stepper’s buttons and icons. | System-defined |
Methods
Method | Return Type | Description |
---|---|---|
setIncrementImage(_:for:) | Void | Sets a custom image for the increment button for a given control state (e.g., .normal , .disabled ). |
setDecrementImage(_:for:) | Void | Sets a custom image for the decrement button for a given control state. |
setBackgroundImage(_:for:) | Void | Sets a background image for the stepper for a given control state. |
setDividerImage(_:forLeftSegmentState:rightSegmentState:) | Void | Sets the divider image between the buttons for specified states. |
addTarget(_:action:for:) | Void | Associates a target and action for control events (e.g., .valueChanged ). |
incrementImage(for:) | UIImage? | Returns the image used for the increment button for a given state. |
decrementImage(for:) | UIImage? | Returns the image used for the decrement button for a given state. |
backgroundImage(forLeftSegmentState:rightSegmentState:) | UIImage? | Returns the divider image for specified segment states. |
Example Usage
Example 1: Basic Stepper with Label
This example creates a stepper to adjust a quantity and updates a label with the current value.
swift
import UIKit
class ViewController: UIViewController {
let stepper = UIStepper()
let label = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
// Configure stepper
stepper.frame = CGRect(x: 100, y: 200, width: 0, height: 0)
stepper.minimumValue = 1
stepper.maximumValue = 10
stepper.stepValue = 1
stepper.value = 1
stepper.addTarget(self, action: #selector(stepperValueChanged(_:)), for: .valueChanged)
// Configure label
label.frame = CGRect(x: 100, y: 150, width: 200, height: 30)
label.textAlignment = .center
label.text = "Quantity: \(Int(stepper.value))"
// Add to view
view.addSubview(stepper)
view.addSubview(label)
}
@objc func stepperValueChanged(_ sender: UIStepper) {
label.text = "Quantity: \(Int(sender.value))"
}
}
Example 2: Custom Stepper with Images and Wrapping
This example customizes the stepper’s appearance with images and enables value wrapping.
swift
import UIKit
class CustomViewController: UIViewController {
let stepper = UIStepper()
let valueLabel = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
// Configure stepper
stepper.frame = CGRect(x: 100, y: 300, width: 0, height: 0)
stepper.minimumValue = 0
stepper.maximumValue = 5
stepper.stepValue = 0.5
stepper.wraps = true
stepper.tintColor = .systemBlue
stepper.setIncrementImage(UIImage(systemName: "plus.circle"), for: .normal)
stepper.setDecrementImage(UIImage(systemName: "minus.circle"), for: .normal)
stepper.addTarget(self, action: #selector(updateLabel(_:)), for: .valueChanged)
// Configure label
valueLabel.frame = CGRect(x: 100, y: 250, width: 200, height: 30)
valueLabel.textAlignment = .center
valueLabel.text = "Value: \(stepper.value)"
// Add to view
view.addSubview(stepper)
view.addSubview(valueLabel)
}
@objc func updateLabel(_ sender: UIStepper) {
valueLabel.text = "Value: \(sender.value)"
}
}
Example 3: Stepper with Continuous Updates
This example demonstrates a stepper with continuous updates and a non-integer step value.
swift
import UIKit
class ContinuousViewController: UIViewController {
let stepper = UIStepper()
let progressLabel = UILabel()
override func viewDidLoad() {
super.viewDidLoad()
// Configure stepper
stepper.frame = CGRect(x: 100, y: 400, width: 0, height: 0)
stepper.minimumValue = 0
stepper.maximumValue = 1
stepper.stepValue = 0.1
stepper.isContinuous = true
stepper.addTarget(self, action: #selector(progressChanged(_:)), for: .valueChanged)
// Configure label
progressLabel.frame = CGRect(x: 100, y: 350, width: 200, height: 30)
progressLabel.textAlignment = .center
progressLabel.text = "Progress: \(Int(stepper.value * 100))%"
// Add to view
view.addSubview(stepper)
view.addSubview(progressLabel)
}
@objc func progressChanged(_ sender: UIStepper) {
progressLabel.text = "Progress: \(Int(sender.value * 100))%"
}
}
Customization
- Tint Color: Modify
tintColor
to change the stepper’s button color (e.g.,stepper.tintColor = .systemRed
). - Custom Images: Use
setIncrementImage(_:for:)
andsetDecrementImage(_:for:)
for custom button icons. - Background: Set a background image with
setBackgroundImage(_:for:)
. - Divider: Customize the divider with
setDividerImage(_:forLeftSegmentState:rightSegmentState:)
.
Accessibility
- Set
accessibilityLabel
(e.g., "Adjust Quantity"). - Update
accessibilityValue
dynamically (e.g., "(Int(stepper.value)) items"). - Ensure
isAccessibilityElement
istrue
for VoiceOver support.
Notes
- Available since iOS 5.0.
- Use
.valueChanged
event to handle value updates. - The stepper’s size is fixed; position it using Auto Layout or frames.
- For SwiftUI, use the
Stepper
view for a declarative alternative.