Swift学习笔记 -- UIButton

UIButton初始化
let testButton:UIButton = UIButton(type: .Custom)
//UIButtonType:Custom System DetailDisclosure InfoLight InfoDark ContactAdd RoundedRect
testButton.frame = CGRect(x: 100, y: 100, width: 100, height: 100)
设置背景色
testButton.backgroundColor = UIColor.redColor()
状态设置
//UIControlState:Normal Highlighted Disabled Selected
        
testButton.setTitle("normal", forState: .Normal)
testButton.setTitle("highlighted", forState: .Highlighted)
testButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
testButton.setTitleColor(UIColor.yellowColor(), forState: .Highlighted)
图片设置
testButton.setImage(UIImage(named: "imageName"), forState: .Normal)
字体设置
testButton.titleLabel?.font = UIFont.systemFontOfSize(12)
点击事件
testButton.addTarget(self, action:#selector(testButtonClick(_:)) , forControlEvents: UIControlEvents.TouchUpInside)
点击方法实现
func testButtonClick(btn:UIButton) -> Void {
        print("click")
    }

你可能感兴趣的:(Swift学习笔记 -- UIButton)