UIButton設置邊框和選擇狀態下的背景顏色

[self LimitedCountBtnInit];
[self LimitedTimeBtnInit];
- (void)LimitedCountBtnInit{
   // self.LimitedCountBtn.layer.borderColor=[UIColor blueColor].CGColor;
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGColorRef color = CGColorCreate(colorSpaceRef, (CGFloat[]){73/255,230/255,255/255,0.2});
    self.LimitedCountBtn.layer.borderColor=color;
   self.LimitedCountBtn.layer.borderWidth=1.0f;
   // self.LimitedCountBtn.layer.cornerRadius=10.0f;
    self.LimitedCountBtn.layer.masksToBounds=YES;
    [self.LimitedCountBtn addTarget:self action:@selector(LimitedCountBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.LimitedCountBtn addObserver:self forKeyPath:@"selected" options:NSKeyValueObservingOptionNew context:nil];
}
- (void)LimitedTimeBtnInit{
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGColorRef color = CGColorCreate(colorSpaceRef, (CGFloat[]){73/255,230/255,255/255,0.2});
    self.LimitedTimeBtn.layer.borderColor=color;
    self.LimitedTimeBtn.layer.borderWidth=1.0f;
    //self.LimitedTimeBtn.layer.cornerRadius=10.0f;
    self.LimitedTimeBtn.layer.masksToBounds=YES;
    [self.LimitedTimeBtn addTarget:self action:@selector(LimitedTimeBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.LimitedTimeBtn addObserver:self forKeyPath:@"selected" options:NSKeyValueObservingOptionNew context:nil];
}

-(void)LimitedCountBtnClicked:(UIButton*)button{
    [self.LimitedTimeBtn setSelected:NO];
    [button setSelected:YES];
    
}
-(void)LimitedTimeBtnClicked:(UIButton*)button{
    [self.LimitedCountBtn setSelected:NO];
    [button setSelected:YES];
    
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    UIButton *button = (UIButton *)object;
    if ([keyPath isEqualToString:@"selected"]) {
        if (button.selected) {
            [button setBackgroundColor:[UIColor blueColor]];
            return;
        }
        [button setBackgroundColor:[UIColor whiteColor]];
    }
}

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章