@property (nonatomic, strong) UIButton *selectBtn;
- (void)viewDidLoad {
[super viewDidLoad];
inttotalColumns =3;
// 每一格的尺寸
CGFloatcellW =60;
CGFloatcellH =35;
// 间隙
CGFloatmargin =10;
//根据格子个数创建对应的框框
for(intindex =0; index <5; index++) {
// 计算行号 和 列号
introw = index / totalColumns;
intcol = index % totalColumns;
//根据行号和列号来确定 子控件的坐标
CGFloatcellX = margin + col * (cellW + margin);
CGFloatcellY = row * (cellH + margin);
UIButton*btn = [[UIButtonalloc]init];
btn.frame=CGRectMake(cellX+15, cellY+170, cellW, cellH);
[btnsetBackgroundColor:ThemeTitleColor];
[btnsetTitle:[NSString stringWithFormat:@"%d",index] forState:UIControlStateNormal];
btn.titleLabel.font=BSFont(14);
btn.tag= index;
if(index==0){
[btnsetTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btnsetBackgroundColor:BSNaviBarColor];
self.selectBtn= btn;
}else{
[btnsetTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
[btnsetBackgroundColor:[UIColor whiteColor]];
btn.layer.borderColor = [UIColor darkGrayColor].CGColor;
btn.layer.borderWidth=1;
}
[btnaddTarget:self action:@selector(btnClickAction:) forControlEvents:UIControlEventTouchUpInside];
// 添加到view 中
[self.view addSubview:btn];
}
}
-(void)btnClickAction:(UIButton *)btn{
if(self.selectBtn){
//未选中
[self.selectBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
[self.selectBtn setBackgroundColor:[UIColor whiteColor]];
self.selectBtn.layer.borderColor = [UIColor darkGrayColor].CGColor;
self.selectBtn.layer.borderWidth = 1;
}
//选中
self.selectBtn= btn;
[self.selectBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.selectBtn setBackgroundColor:BSNaviBarColor];
self.selectBtn.layer.borderColor = [UIColor clearColor].CGColor;
self.selectBtn.layer.borderWidth = 0.1;
}