for循环创建UIButton,如何去修改边框颜色

来源:互联网 发布:org.apache.cxf maven 编辑:程序博客网 时间:2024/06/09 14:30

看一下效果图,类似于淘宝加入购物车,我用的是UIButton


颜色颜色Button和型号一样:

1.创建UIButton

for (int i = 0; i < 6; i++) {
        self.selectColorBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        if (i > 4) {
        self.selectColorBtn.frame = CGRectMake(70 * widthScale + 60 * widthScale * (i-5)  , 50 * widthScale, 50 * widthScale, 30 *widthScale);
        }else{
        self.selectColorBtn.frame = CGRectMake(70 * widthScale + 60 * widthScale * i  , 10 * widthScale, 50 * widthScale, 30 *widthScale);
        }
        [self.selectColorBtn setTitle:@"暗灰色" forState:UIControlStateNormal];
        [self.selectColorBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.selectColorBtn.tag = 100 + i;
        self.selectColorBtn.titleLabel.font = [UIFont systemFontOfSize:15 * widthScale];
        [self.selectColorBtn.layer setBorderWidth:1.0];
        [self.selectColorBtn.layer setBorderColor:[gray CGColor]];
        [self.selectColorBtn addTarget:self action:@selector(selectColorAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.twoView addSubview:self.selectColorBtn];
        
        
    }

2.按钮点击事件

- (void)selectColorAction:(UIButton *)btn{

//将Button颜色全部修改为灰色

    for (int i = 0; i < 6; i++) {

//通过tag值获取UIbutton

        UIButton *btn = (UIButton *)[self.twoView viewWithTag:100 + i];
        [btn.layer setBorderColor:[gray CGColor]];
    }

//获取当前UIButton

    UIButton *nowBtn = btn;
    switch (btn.tag) {
        case 100:
         
        [nowBtn.layer setBorderColor:[ThemeColors CGColor]];
            break;
        case 101:
            [nowBtn.layer setBorderColor:[ThemeColors CGColor]];
            break;
        case 102:
           
            [nowBtn.layer setBorderColor:[ThemeColors CGColor]];
            break;
        case 103:
            [nowBtn.layer setBorderColor:[ThemeColors CGColor]];
            break;
        case 104:
            [nowBtn.layer setBorderColor:[ThemeColors CGColor]];
            break;
        case 105:
            [nowBtn.layer setBorderColor:[ThemeColors CGColor]];
            break;
        default:
            break;
    }
    




0 0