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;
   }                                      
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容