UIButton依据不同的状态设置layer

UIButton根据不同的状态设置layer 这需要用到KVO,监听button的highlighted属性的变化,在监听回调里根据监听到的属性值设置layer
设置监听如下

[button  addObserver:self forKeyPath:@"highlighted" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:NULL];

监听回调如下

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {    

     UIButton * bt = (UIButton* )object;   
     if([keyPath isEqualToString:@"highlighted"])  
    {
     if(bt.state == UIControlStateNormal) {
        bt.layer.bordColor  = [UIColor redColor].CGColor;
     } else{   
        bt.layer.bordColor  = [UIColor black].CGColor;
   }                                      
}

你可能感兴趣的:(UIButton依据不同的状态设置layer)