ios-oc-uibutton 按钮点击更改背景色处理

oc的uibutton按钮点击更改背景色处理

新加一个button

要求圆角 同时点击时变色,我这里采用颜色绘图方式,更改其背景图。
1.注意[btn.layer setMasksToBounds:YES];
这个可以避免四个圆角出现白边。
2.btn.layer.cornerRadius = 5.0f; 这个用于设置圆角
代码.

// 新建一个button
            UIButton *btn  = [UIButton buttonWithType:UIButtonTypeCustom];
            
            btn.selected = YES;
            [btn addTarget:weakself action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            [btn setImage:[UIImage imageNamed:@"icon_meet_add"] forState:UIControlStateNormal];
            [btn  setTitle:@"添加到日程" forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont fontWithName:kMediumFont size:NormalFontSize];
            [btn setTitleColor:[UIColor colorWithHexString:@"ea3939"] forState:UIControlStateNormal];
            [btn setBackgroundColor:[UIColor colorWithHexString:@"ffffff"]];
//            [btn addTarget:weakself action:@selector(buttonBackGroundHighlighted:) forControlEvents:UIControlEventTouchDown];
//            [btn addTarget:weakself action:@selector(buttonBackGroundNormal:) forControlEvents:UIControlEventTouchUpInside];
            [btn.layer setBorderColor:[UIColor colorWithHexString:@"ea3939"].CGColor];
            [btn setBackgroundImage:[weakself imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
            [btn setBackgroundImage:[weakself imageWithColor:[UIColor colorWithHexString:@"ffebeb"]] forState :UIControlStateSelected|UIControlStateHighlighted];
            [btn.layer setMasksToBounds:YES];
            btn.layer.cornerRadius = 5.0f;
            [btn.layer setBorderWidth:0.5];
            btn.imageEdgeInsets = UIEdgeInsetsMake(0, btn.frame.size.width - btn.imageView.frame.origin.x - btn.imageView.frame.size.width - 5, 0, 0);
            btn.titleEdgeInsets = UIEdgeInsetsMake(0, -(btn.frame.size.width - btn.imageView.frame.size.width ), 0, 0);
            [addCalender addSubview:btn];
            [btn makeConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(@50);
                make.width.equalTo(@(SCREEN_WIDTH - 60));
                make.top.equalTo(addCalender.top).with.offset(40);
                make.centerX.equalTo(addCalender.centerX);
            }];

使用的方法

- (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}
//  button1普通状态下的背景色
- (void)buttonBackGroundNormal:(UIButton *)sender
{
    sender.backgroundColor = [UIColor whiteColor];
}

//  button1高亮状态下的背景色
- (void)buttonBackGroundHighlighted:(UIButton *)sender
{
    sender.backgroundColor = [UIColor colorWithHexString:@"ffebeb"];
}

点击前的效果
点击后的效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值