Swift UI - 开关按钮(UISwitch)

注:代码已升级至Swift4

创建UISwitch,以及监听值的改变

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import UIKit

class ViewController: UIViewController {
var uiswitch:UISwitch!;

override func viewDidLoad() {
super.viewDidLoad()

uiswitch = UISwitch()
//设置位置(开关大小无法设置)
uiswitch.center = CGPoint(x:100, y:50)
//设置默认值
uiswitch.isOn = true;
uiswitch.addTarget(self, action: #selector(switchDidChanged), for:.valueChanged)
self.view.addSubview(uiswitch);
}

func switchDidChanged() {
//打印当前值
print(uiswitch.isOn)
}
}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :