import UIKit
class ViewController: UIViewController {
var uiswitch: UISwitch?
override func viewDidLoad() {
super.viewDidLoad()
uiswitch = UISwitch()
// 设置位置 (开关无法设置大小)
uiswitch?.center = CGPoint(x: 100, y: 50)
self.view.addSubview(uiswitch!)
// 设置默认值
uiswitch?.isOn = true
uiswitch?.addTarget(self, action: #selector(switchDidChange), for: .valueChanged)
}
func switchDidChange(){
// 打印当前值
print(uiswitch!.isOn)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}