Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

UIStepper

UIStepper increments or decrements a value.

Example

UIStepper

Instructions

Create

  1. Open Xcode
  2. Click File
  3. Click New
  4. Click Project...
  5. Under iOS, click Application
  6. Click Single View Application
  7. Click Next
  8. After Product Name, type your product name
  9. After Organization Name, type your organization name or leave blank
  10. After Organization Identifier, type your organization identifier
  11. After Language, select Swift
  12. After Devices, select Universal
  13. Uncheck Use Core Data
  14. Click Next
  15. Select a folder
  16. After Source Control, uncheck Create Git repository on
  17. Click Create

Edit ViewController.swift

  1. Open ViewController.swift
  2. Copy and paste code

Edit Main.storyboard

  1. Open Main.storyboard
  2. Click View
  3. Click Utilities
  4. Click Show Object Library
  5. Drag Stepper to View
  6. Click View
  7. Click Utilities
  8. Click Show Connections Inspector
  9. Under Sent Events, after Value Changed, drag + to View Controller
  10. Click valueChanged:
  11. Under Referencing Outlets, after New Referencing Outlet, drag + to View Controller
  12. Click interfaceBuilderStepper

Add Image

  1. Click File
  2. Click Add Files to "UIStepper" ...
  3. Select image
  4. After Destination, check Copy items into destination group's folder (if needed)
  5. After Folders, select Create groups for any added folders
  6. After Add to targets, check UIStepper
  7. Click Add

Run

  1. Click Product
  2. 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)
    }
}

Apple UIKit

HomeMenu