ios-UIbutton
//创建一个button
let button:UIbutton = UIbutton(type:UIbutoonType.Custom)
//添加到视图上
self.view.addsubview(button)
//添加事件
button.addTarget(self,action:Selector("click:"),forControlevents:UIControlEvents.TouchupInside)
//设置按钮的背景图片
button.setBackgroundImage(named:"1.jpg"),forstate:UIcontrolState.normal)
//设置按钮图片
button.setImage(UIimage(named:"1.jpg"),forstate:UIcontrolstate.Normal)
//设置button内容区域偏移量
button.contentEdgeinsets = UIEdgeInsetsMake(-10,-10,0,0)
//开启按钮的点击视觉效果
button.showsTouchWhenHighlighted = true
//设置高亮状态的按钮标题
button.setTitle("新标题",forState:UIcontrolState.Highlighted)
func click(btn:UIbutton)
{
print("按钮被点击了")
btn.backgroundColor = UIcolor(red:(Cgfloat)random()%255)/255 ,
green:(CGfloat)(random()%255)/255,blue: (CGfloat)(random(%255)/255,alpha:1)
}