swift中UISwitch的使用

来源:互联网 发布:乐视视频下载mac 编辑:程序博客网 时间:2024/05/29 08:05
  1. // 实例化(注意:默认宽高是 51 x 31。指定大小无效)  
  2. let switchview = UISwitch(frame: CGRectMake(10.0, 10.0, 0.0, 0.0))  
  3. self.view.addSubview(switchview)  
  4. switchview.backgroundColor = UIColor.yellowColor()  
  5.           
  6. // 打开时背景色  
  7. switchview.onTintColor = UIColor.brownColor()  
  8. // 开关块颜色  
  9. switchview.thumbTintColor = UIColor.blueColor()  
  10. // 关闭时边框颜色  
  11. switchview.tintColor = UIColor.orangeColor()  
  12.           
  13. // 开关图标设置(设置后无效??)  
  14. switchview.onImage = UIImage(named: "normalImage")  
  15. switchview.offImage = UIImage(named: "hightImage")  
  16.           
  17. // 初始化开,或关  
  18. switchview.setOn(false, animated: true)  
  19.           
  20. // 响应事件  
  21. switchview.addTarget(self, action: Selector("switchValueChange:"), forControlEvents: UIControlEvents.ValueChanged)  
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // MARK: - switchValueChange  
  2. func switchValueChange(sender:UISwitch)  
  3. {  
  4.         let openStatus = sender.on  
  5.         if openStatus  
  6.         {  
  7.             self.view.backgroundColor = UIColor.cyanColor()  
  8.         }  
  9.         else  
  10.         {  
  11.             self.view.backgroundColor = UIColor.whiteColor()  
  12.         }  
  13. }  

0 0
原创粉丝点击