swift中UIButton的使用

来源:互联网 发布:中信淘宝网联名信用卡 编辑:程序博客网 时间:2024/04/29 15:45
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. let button = UIButton(type: UIButtonType.Custom)  
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 添加到父视图,并设置frame  
  2. self.view.addSubview(button)  
  3. button.frame = CGRectMake(10.010.0200.040.0)  
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 背景属性设置  
  2. button.backgroundColor = UIColor.yellowColor()  
  3. // button.setBackgroundImage(UIImage(named: "normalImage"), forState: UIControlState.Normal)  
  4. // button.setBackgroundImage(UIImage(named: "hightImage"), forState: UIControlState.Highlighted)  
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 图标设置  
  2. button.setImage(UIImage(named: "normalImage"), forState: UIControlState.Normal)  
  3. button.setImage(UIImage(named: "hightImage"), forState: UIControlState.Highlighted)  
  4. button.imageEdgeInsets = UIEdgeInsetsMake(0.0, -50.00.00.0)  
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 标题设置  
  2. button.setTitle("button", forState: UIControlState.Normal)  
  3. button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)  
  4. button.setTitleColor(UIColor.redColor(), forState: UIControlState.Highlighted)  
  5. button.setTitleShadowColor(UIColor.greenColor(), forState: UIControlState.Normal)  
  6. button.titleLabel?.font = UIFont(name: "GillSans", size20.0)  
  7. button.titleEdgeInsets = UIEdgeInsetsMake(0.00.00.0, -50.0)  
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. button.selected = false  
  2. button.enabled = true  
  3. button.userInteractionEnabled = true  
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 响应事件  
  2. button.addTarget(self, action: Selector("buttonClick:"), forControlEvents: UIControlEvents.TouchUpInside)  
[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. // 响应事件  
  2. func buttonClick(button:UIButton) -> Void  
  3. {  
  4.         button.selected = !button.selected  
  5.         if button.selected == true  
  6.         {  
  7.             print("选中按钮")  
  8.               
  9.             let buttonTitle = button.titleLabel?.text  
  10.             print(buttonTitle)  
  11.         }  
  12.         else  
  13.         {  
  14.             print("未选中按钮")  
  15.         }  
  16. }  

 
0 0