纯代码创建控件之UIButton

UIButton 设置fram

let textButton = UIButton(frame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height))

设置title        

textButton.setTitle(“button”,forState: UIControlState.Normal)

设置button 的title color

textButton.setTitleColor(redColor, forState: UIControlState.Normal)

设置button的显示图片

var backImage = UIImage(named:“icon114.png”)

textButton.setImage(backImage, forState : UIControlState.Normal)

给button添加事件

textButton.addTarget(self,action:“buttonActions:”,forControlEvents:UIControlEvents.TouchUpInside)

给button设置背景颜色

var whitColor = UIColor(red:1.0,green:1.0,blue:1.0,alpha:1.0)

textButton.backgroundColor = whitColor

self.view.addSubview(textButton)




响应button的事件

UIButton后面的 ”!“ 表示sender可以是由UIButton继承来的任意子类。

func buttonActions(sender: UIButton!) {

        println(“tapped button”)

    }

你可能感兴趣的:(纯代码创建控件之UIButton)