点击后重绘自定义UIButton

时间:2015-07-06 22:18:09

标签: ios swift uibutton

我有一个自定义的UIButton,我想在点击后重绘 - 我想改变它的颜色。这是代码:

class DayButtons: UIButton {

    var isPressed: Bool = false

    var color = UIColor.whiteColor()

        override func drawRect(rect: CGRect) {
        let cornerR = CGFloat(5)       
        var path = UIBezierPath(roundedRect: rect, cornerRadius: cornerR)
        color.setFill()
        path.fill()
    }
}

有没有办法在我的主线程中再次调用drawRect?

1 个答案:

答案 0 :(得分:7)

您应该在按钮上拨打setNeedsDisplay(),然后为您拨打drawRect()