纯代码创建控件之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”)

    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容