1,按钮的创建
(1)按钮有下面四种类型:
let btn=UIButton(type: .ContactAdd)
(2)对于Custom定制类型按钮,代码可简化为:
let button=UIButton(frame: CGRect(x: 0, y: 0, 100, height: 100))
button.backgroundColor=UIColor.purpleColor()
// button.frame=CGRectMake(0, 0, 100, 100)
button.titleLabel!.font=UIFont.systemFontOfSize(20)//文字的大小
button.setTitle("Swift", forState:UIControlState.Normal)
button.setTitle("Button", forState: UIControlState.Highlighted)
button.setTitleColor(.redColor(), forState: UIControlState.Normal)//普通状态下文字的颜色
button.setTitleColor(UIColor.yellowColor(), forState: UIControlState.Highlighted)
button.setTitleShadowColor(UIColor.whiteColor(), forState: UIControlState.Normal)//普通状态下文字阴影的颜色
button.setTitleShadowColor(UIColor.blackColor(), forState: UIControlState.Highlighted)
button.setImage(UIImage(named: "Icon-72"), forState: UIControlState.Normal)//设置图标
button.adjustsImageWhenHighlighted=false//使触摸模式下按钮也不会变暗
button.setBackgroundImage(UIImage(named: "1.jpg"), forState: UIControlState.Normal)//背景图片
button.addTarget(self, action: Selector("clicekdBtn:"), forControlEvents: .TouchUpInside)
self.view.addSubview(button)
}
func clicekdBtn(button:UIButton){
print(button.titleForState(UIControlState.Normal))
}