Skip to content

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

PropertyTypeDescriptionDefault Value
valueDoubleThe current numeric value of the stepper.0.0
minimumValueDoubleThe lowest possible value.0.0
maximumValueDoubleThe highest possible value.100.0
stepValueDoubleThe increment/decrement amount per tap.1.0
isContinuousBoolIf true, sends continuous updates while a button is held.true
autorepeattrueIf true, holding a button continuously increments/decrements the value.Always true
wrapsBoolIf true, the value wraps around when it exceeds the minimum/maximum.false
tintColorUIColor?The color of the stepper’s buttons and icons.System-defined

Methods

MethodReturn TypeDescription
setIncrementImage(_:for:)VoidSets a custom image for the increment button for a given control state (e.g., .normal, .disabled).
setDecrementImage(_:for:)VoidSets a custom image for the decrement button for a given control state.
setBackgroundImage(_:for:)VoidSets a background image for the stepper for a given control state.
setDividerImage(_:forLeftSegmentState:rightSegmentState:)VoidSets the divider image between the buttons for specified states.
addTarget(_:action:for:)VoidAssociates 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:) and setDecrementImage(_: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 is true 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.

Resources

Released under the MIT License.