开关组件是非常常见的一个控件,在默认状态下,开关组件是关闭状态的
通过坐标大小来创建
可以通过setOn函数来设定默认状态是开还是关
可以为switch添加响应事件
自定义开关的外观
代码如下
class ViewController: UIViewController {
override func viewDidLoad() {
//自定义UIswitch 开关按键 ,用方位来创建他的相对位置
let mainSwitch = UISwitch(frame: CGRect(x: 100, y: 100, width: 0, height: 0))
//设置关闭时的颜色状态,可惜只有在边缘那里显示出来,但是在进行状态切换的时候,颜色的过度是很明显的
mainSwitch.tintColor = UIColor.redColor()
//小按钮的颜色
mainSwitch.thumbTintColor = UIColor.greenColor()
//打开状态时的颜色
mainSwitch.onTintColor = UIColor.brownColor()
func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
}
最终效果如图