UISWITCH
UISwitch displays an on/off switch.
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 Switch 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 interfaceBuilderSwitch
ADD IMAGE
- Click File
- Click Add Files to "UISwitch" ...
- 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 UISwitch
- Click Add
RUN
- Click Product
- 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)
}
}