美文网首页程序员iOS Developer
Swift学习笔记 -- UIButton

Swift学习笔记 -- UIButton

作者: 偶然中的必然 | 来源:发表于2016-07-08 15:12 被阅读95次
    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

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