Change UIButton border color on highlight

后端 未结 6 789
礼貌的吻别
礼貌的吻别 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:31

    You can override setHighlighted and setSelected methods in UIButton subclass. From there you just tweak border color like following:

    - (void)setHighlighted:(BOOL)highlighted {
    
        [super setHighlighted:highlighted];
    
        [self tweakState:highlighted];
    }
    
    - (void)setSelected:(BOOL)selected {
    
        [super setSelected:selected];
    
        [self tweakState:selected];
    }
    
    - (void)tweakState:(BOOL)state {
    
        if (state) {
            self.layer.borderColor = [_highlightedBorderColor CGColor];
        }
        else {
            self.layer.borderColor = [_defaultBorderColor CGColor];
        }
    }
    

提交回复
热议问题