美文网首页
纯代码创建控件之UIButton

纯代码创建控件之UIButton

作者: 彧哥哥 | 来源:发表于2017-05-21 04:00 被阅读0次
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

      本文链接:https://www.haomeiwen.com/subject/pqdmxxtx.html