前端之家收集整理的这篇文章主要介绍了
swift中UISwitch的使用,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
// 实例化(注意:默认宽高是 51 x 31。指定大小无效)
let switchview = UISwitch(frame: CGRectMake(10.0,10.0,0.0,0.0))
self.view.addSubview(switchview)
switchview.backgroundColor = UIColor.yellowColor()
// 打开时背景色
switchview.onTintColor = UIColor.brownColor()
// 开关块颜色
switchview.thumbTintColor = UIColor.blueColor()
// 关闭时边框颜色
switchview.tintColor = UIColor.orangeColor()
// 开关图标设置(设置后无效??)
switchview.onImage = UIImage(named: "normalImage")
switchview.offImage = UIImage(named: "hightImage")
// 初始化开,或关
switchview.setOn(false,animated: true)
// 响应事件
switchview.addTarget(self,action: Selector("switchValueChange:"),forControlEvents: UIControlEvents.ValueChanged)
// MARK: - switchValueChange
func switchValueChange(sender:UISwitch)
{
let openStatus = sender.on
if openStatus
{
self.view.backgroundColor = UIColor.cyanColor()
}
else
{
self.view.backgroundColor = UIColor.whiteColor()
}
}