UIStepper
UIStepper increments or decrements a value.
Example
Instructions
Create
- Open Xcode
- Click File
- Click New
- Click Project...
- Under iOS, click Application
- Click Single View Application
- Click Next
- After Product Name, type your product name
- After Organization Name, type your organization name or leave blank
- After Organization Identifier, type your organization identifier
- After Language, select Swift
- After Devices, select Universal
- Uncheck Use Core Data
- Click Next
- Select a folder
- After Source Control, uncheck Create Git repository on
- Click Create
Edit ViewController.swift
- Open ViewController.swift
- Copy and paste code
Edit Main.storyboard
- Open Main.storyboard
- Click View
- Click Utilities
- Click Show Object Library
- Drag Stepper to View
- Click View
- Click Utilities
- Click Show Connections Inspector
- Under Sent Events, after Value Changed, drag + to View Controller
- Click valueChanged:
- Under Referencing Outlets, after New Referencing Outlet, drag + to View Controller
- Click interfaceBuilderStepper
Add Image
- Click File
- Click Add Files to "UIStepper" ...
- Select image
- After Destination, check Copy items into destination group's folder (if needed)
- After Folders, select Create groups for any added folders
- After Add to targets, check UIStepper
- Click Add
Run
- Click Product
- Click Run
Code
import UIKit class ViewController: UIViewController { @IBOutlet weak var interfaceBuilderStepper: UIStepper! var label: UILabel! override func prefersStatusBarHidden() -> Bool { return true } override func viewDidLoad() { super.viewDidLoad() self.label = UILabel() self.label.setTranslatesAutoresizingMaskIntoConstraints(false) self.label.text = NSString(format: "%f", self.interfaceBuilderStepper.value) self.label.textAlignment = NSTextAlignment.Center self.view.addSubview(self.label) self.view.removeConstraints(self.view.constraints()) self.view.addConstraint(NSLayoutConstraint(item: self.label, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0)) self.view.addConstraint(NSLayoutConstraint(item: self.interfaceBuilderStepper, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.label, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)) let valueStepper = UIStepper() valueStepper.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged) valueStepper.setTranslatesAutoresizingMaskIntoConstraints(false) valueStepper.value = 0 self.view.addSubview(valueStepper) self.view.addConstraint(NSLayoutConstraint(item: valueStepper, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.interfaceBuilderStepper, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)) let minimumValueStepper = UIStepper() minimumValueStepper.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged) minimumValueStepper.minimumValue = -10 minimumValueStepper.setTranslatesAutoresizingMaskIntoConstraints(false) minimumValueStepper.value = -10 self.view.addSubview(minimumValueStepper) self.view.addConstraint(NSLayoutConstraint(item: minimumValueStepper, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: valueStepper, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)) let maximumValueStepper = UIStepper() maximumValueStepper.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged) maximumValueStepper.maximumValue = 10 maximumValueStepper.setTranslatesAutoresizingMaskIntoConstraints(false) maximumValueStepper.value = 10 self.view.addSubview(maximumValueStepper) self.view.addConstraint(NSLayoutConstraint(item: maximumValueStepper, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: minimumValueStepper, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)) let stepValueStepper = UIStepper() stepValueStepper.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged) stepValueStepper.setTranslatesAutoresizingMaskIntoConstraints(false) stepValueStepper.stepValue = 11.111111 self.view.addSubview(stepValueStepper) self.view.addConstraint(NSLayoutConstraint(item: stepValueStepper, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: maximumValueStepper, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)) let wrapsStepper = UIStepper() wrapsStepper.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged) wrapsStepper.setTranslatesAutoresizingMaskIntoConstraints(false) wrapsStepper.wraps = true self.view.addSubview(wrapsStepper) self.view.addConstraint(NSLayoutConstraint(item: wrapsStepper, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: stepValueStepper, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)) let autorepeatStepper = UIStepper() autorepeatStepper.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged) autorepeatStepper.autorepeat = false autorepeatStepper.setTranslatesAutoresizingMaskIntoConstraints(false) self.view.addSubview(autorepeatStepper) self.view.addConstraint(NSLayoutConstraint(item: autorepeatStepper, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: wrapsStepper, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)) let continuousStepper = UIStepper() continuousStepper.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged) continuousStepper.continuous = false continuousStepper.setTranslatesAutoresizingMaskIntoConstraints(false) self.view.addSubview(continuousStepper) self.view.addConstraint(NSLayoutConstraint(item: continuousStepper, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: autorepeatStepper, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)) let tintColorStepper = UIStepper() tintColorStepper.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged) tintColorStepper.setTranslatesAutoresizingMaskIntoConstraints(false) tintColorStepper.tintColor = UIColor.redColor() self.view.addSubview(tintColorStepper) self.view.addConstraint(NSLayoutConstraint(item: tintColorStepper, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: continuousStepper, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)) let dividerImageStepper = UIStepper() dividerImageStepper.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged) dividerImageStepper.setDividerImage(UIImage(named: "HappyFace"), forLeftSegmentState: UIControlState.Highlighted, rightSegmentState: UIControlState.Normal) dividerImageStepper.setDividerImage(UIImage(named: "HappyFace"), forLeftSegmentState: UIControlState.Normal, rightSegmentState: UIControlState.Normal) dividerImageStepper.setDividerImage(UIImage(named: "HappyFace"), forLeftSegmentState: UIControlState.Normal, rightSegmentState: UIControlState.Highlighted) dividerImageStepper.setTranslatesAutoresizingMaskIntoConstraints(false) self.view.addSubview(dividerImageStepper) self.view.addConstraint(NSLayoutConstraint(item: dividerImageStepper, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: tintColorStepper, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)) let backgroundImageStepper = UIStepper() backgroundImageStepper.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged) backgroundImageStepper.setBackgroundImage(UIImage(named: "HappyFace"), forState: UIControlState.Disabled) backgroundImageStepper.setBackgroundImage(UIImage(named: "HappyFace"), forState: UIControlState.Highlighted) backgroundImageStepper.setBackgroundImage(UIImage(named: "HappyFace"), forState: UIControlState.Normal) backgroundImageStepper.setTranslatesAutoresizingMaskIntoConstraints(false) self.view.addSubview(backgroundImageStepper) self.view.addConstraint(NSLayoutConstraint(item: backgroundImageStepper, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: dividerImageStepper, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)) let decrementImageStepper = UIStepper() decrementImageStepper.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged) decrementImageStepper.setDecrementImage(UIImage(named: "HappyFace"), forState: UIControlState.Normal) decrementImageStepper.setTranslatesAutoresizingMaskIntoConstraints(false) self.view.addSubview(decrementImageStepper) self.view.addConstraint(NSLayoutConstraint(item: decrementImageStepper, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: backgroundImageStepper, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)) let incrementImageStepper = UIStepper() incrementImageStepper.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged) incrementImageStepper.setIncrementImage(UIImage(named: "HappyFace"), forState: UIControlState.Normal) incrementImageStepper.setTranslatesAutoresizingMaskIntoConstraints(false) self.view.addSubview(incrementImageStepper) self.view.addConstraint(NSLayoutConstraint(item: incrementImageStepper, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: decrementImageStepper, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)) } @IBAction func valueChanged(sender: UIStepper!) { self.label.text = NSString(format: "%f", sender.value) } }