UISwitch

UISwitch displays an on/off switch.

Example

UISwitch

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 Switch 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 interfaceBuilderSwitch

Add Image

  1. Click File
  2. Click Add Files to "UISwitch" ...
  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 UISwitch
  7. Click Add

Run

  1. Click Product
  2. Click Run

Code

import UIKit

class ViewController: UIViewController
{
  @IBOutlet weak var interfaceBuilderSwitch: UISwitch!
  
  override func prefersStatusBarHidden() -> Bool
  {
    return true
  }
  override func viewDidLoad()
  {
    super.viewDidLoad()
    
    let onSwitch = UISwitch()
    onSwitch.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged)
    onSwitch.on = true
    onSwitch.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.view.addSubview(onSwitch)
    self.view.removeConstraints(self.view.constraints())
    self.view.addConstraint(NSLayoutConstraint(item: onSwitch, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.interfaceBuilderSwitch, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let onAnimatedSwitch = UISwitch()
    onAnimatedSwitch.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged)
    onAnimatedSwitch.setOn(true, animated: true)
    onAnimatedSwitch.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.view.addSubview(onAnimatedSwitch)
    self.view.addConstraint(NSLayoutConstraint(item: onAnimatedSwitch, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: onSwitch, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let thumbTintColorSwitch = UISwitch()
    thumbTintColorSwitch.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged)
    thumbTintColorSwitch.setTranslatesAutoresizingMaskIntoConstraints(false)
    thumbTintColorSwitch.thumbTintColor = UIColor.redColor()
    self.view.addSubview(thumbTintColorSwitch)
    self.view.addConstraint(NSLayoutConstraint(item: thumbTintColorSwitch, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: onAnimatedSwitch, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let tintColorSwitch = UISwitch()
    tintColorSwitch.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged)
    tintColorSwitch.setTranslatesAutoresizingMaskIntoConstraints(false)
    tintColorSwitch.tintColor = UIColor.redColor()
    self.view.addSubview(tintColorSwitch)
    self.view.addConstraint(NSLayoutConstraint(item: tintColorSwitch, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: thumbTintColorSwitch, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let onTintColorSwitch = UISwitch()
    onTintColorSwitch.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged)
    onTintColorSwitch.on = true
    onTintColorSwitch.onTintColor = UIColor.redColor()
    onTintColorSwitch.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.view.addSubview(onTintColorSwitch)
    self.view.addConstraint(NSLayoutConstraint(item: onTintColorSwitch, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: tintColorSwitch, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    /*
    let offImageSwitch = UISwitch()
    offImageSwitch.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged)
    offImageSwitch.offImage = UIImage(named: "HappyFace") // no effect as of iOS7
    offImageSwitch.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.view.addSubview(offImageSwitch)
    self.view.addConstraint(NSLayoutConstraint(item: offImageSwitch, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: onTintColorSwitch, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

    let onImageSwitch = UISwitch()
    onImageSwitch.addTarget(self, action: "valueChanged:", forControlEvents: UIControlEvents.ValueChanged)
    onImageSwitch.on = true
    onImageSwitch.onImage = UIImage(named: "HappyFace") // no effect as of iOS7
    onImageSwitch.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.view.addSubview(onImageSwitch)
    self.view.addConstraint(NSLayoutConstraint(item: onImageSwitch, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: offImageSwitch, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    */
  }
  @IBAction func valueChanged(sender: UISwitch!)
  {
    var string = "false"
    
    if (sender.on)
    {
      string = "true"
    }
    
    //let alertView = UIAlertView(title: string, message: nil, delegate: nil, cancelButtonTitle: "Cancel")
    //alertView.show()
    
    let alertController = UIAlertController(title: string, message: nil, preferredStyle: UIAlertControllerStyle.Alert)
    alertController.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))
    self.presentViewController(alertController, animated: true, completion: nil)

  }
}

External

Apple iOS Developer Library