swift-UIButton

oc转向swift势在必行, 我也开始熟悉swift语法了, 希望下一个项目能用swift来写.

 1  // MARK: 1. 创建btn
 2     private func creatBtn(content:NSString) -> UIButton{
 3         /// 创建btn
 4 //        let btn:UIButton = UIButton(type:.ContactAdd)
 5         
 6         let btn = UIButton.init(frame: CGRectMake(100, 100, 100, 100))
 7         btn.backgroundColor = UIColor .redColor()
 8 
 9         btn.center = self.view.center
10         /**
11          设置title
12          
13          - parameter forState: 状态
14          */
15         btn.setTitle("normal", forState: UIControlState.Normal)
16         btn.setTitle("highlighted", forState: UIControlState.Highlighted)
17         btn.setTitle("disabled", forState: UIControlState.Disabled)
18         
19         /**
20          设置titleColor
21          
22          - parameter <Tcolor:  color
23          - parameter forState: state
24          */
25         btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
26         btn.setTitleColor(UIColor.redColor(), forState: .Highlighted)
27         btn.setTitleColor(UIColor.lightGrayColor(), forState: .Disabled)
28         
29         /**
30          设置img
31          
32          - parameter forState: normal
33          */
34         btn.setBackgroundImage(UIImage(named: "new_feature_1.jpg"), forState: .Normal)
35         
36 //        btn.setImage(UIImage(named: "new_feature_1.jpg"), forState: .Normal)
37         btn.adjustsImageWhenHighlighted = false;
38         btn.adjustsImageWhenDisabled = false;
39     
40         
41         btn.addTarget(self, action: #selector(tapped(_:)),forControlEvents:.TouchUpInside)
42         
43         
44         self.view.addSubview(btn)
45         
46         return btn
47     }
48     
49     /**
50      btn -- 点击事件
51      
52      - parameter button: btn
53      */
54     func tapped(button:UIButton){
55         print(button.titleForState(.Normal))
56     }

 

posted @ 2016-05-16 15:43  guangleijia  阅读(666)  评论(0编辑  收藏  举报