Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change UIButton border when it's disabled

I want to hide the border of a button when it's disabled. When it's enabled, I set the border like this:

_btnInit.layer.borderWidth = 1;
_btnInit.layer.borderColor = [[UIColor grayColor]CGColor];

And when it's disabled, I want to delete the border. enter image description here

Like in the picture. The left is in the disabled state and the right is at the normal state.

like image 399
edjoker Avatar asked Jun 24 '26 01:06

edjoker


1 Answers

Subclass UIButton and override isEnabled property like this,

override var isEnabled: Bool {
    didSet {
        if isEnabled {
            self.layer.borderColor = UIColor.black.cgColor
            self.layer.borderWidth = 1.0
        } else {
            self.layer.borderWidth = 0.0
        }
    }
}
like image 152
PPL Avatar answered Jun 26 '26 18:06

PPL