onswitch = UISwitch()
view.addSubview(onswitch)
// Apple官方说明,对UISwitch设置大小是无效的,其永远保持在(51,31)的大小
onswitch.mas_makeConstraints { (make) in
make?.left.equalTo()(view.mas_left)?.offset()(RESIZE(size: 100))
make?.top.equalTo()(nameTextFiled.mas_bottom)?.offset()(RESIZE(size: 40))
make?.width.mas_equalTo()(RESIZE(size: 100));
make?.height.mas_equalTo()(RESIZE(size: 40))
}
// onswitch.isOn = false
onswitch.transform = CGAffineTransform(scaleX: 0.75, y: 0.75);//可以使用transform修改switch的大小
onswitch.setOn(true, animated: true)
onswitch.thumbTintColor = UIColor.red//滑块上小圆点的颜色
onswitch.onTintColor = UIColor.green//设置开启状态显示的颜色
onswitch.tintColor = UIColor.black//设置关闭状态的颜色
onswitch.onImage = UIImage(named: "1")
onswitch.offImage = UIImage(named: "2")
onswitch.addTarget(self, action: #selector(switchClick), for: .valueChanged)
实现点击方法
@objc func switchClick(){
print(onswitch.isOn)
}
网友评论