Change UIButton border color on highlight

后端 未结 6 786
礼貌的吻别
礼貌的吻别 2021-02-03 23:53

I\'ve got a simple custom UIButton, to which I added:

button.layer.bordercolor = [[UIColor blueColor]CGColor];

However, I want to change the .b

6条回答
  •  失恋的感觉
    2021-02-04 00:29

    The SWIFT 2.x answer to your problem:

    ➜ Just override the highlighted property with "didSet" observer.

    override var highlighted: Bool {
        didSet {
    
            switch highlighted {
            case true:
                layer.borderColor = UIColor.lightGrayColor().CGColor
            case false:
                layer.borderColor = UIColor.blackColor().CGColor
            }
        }
    }
    

    Swift 3:

    override var isHighlighted: Bool {
        didSet {
    
            switch isHighlighted {
            case true:
                layer.borderColor = UIColor.lightGray.cgColor
            case false:
                layer.borderColor = UIColor.black.cgColor
            }
        }
    }
    

提交回复
热议问题