UISwitch的使用
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.setupSwitch()
}
func setupSwitch() {
// 创建switch
let switch_ = UISwitch()
switch_.center = CGPoint(x: 100, y: 100)
// 设置默认值
switch_.isOn = true
// 添加事件
switch_.addTarget(self, action: #selector(switchChange), for: .valueChanged)
self.view.addSubview(switch_)
}
func switchChange() {
print("哈哈")
}
}