@property (nonatomic, strong) UIButton *selectBtn;
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ?inttotalColumns =3;
? ? //? 每一格的尺寸
? ? CGFloatcellW =60;
? ? CGFloatcellH =35;
? ? // 間隙
? ? CGFloatmargin =10;
? ? //根據(jù)格子個(gè)數(shù)創(chuàng)建對(duì)應(yīng)的框框
? ? for(intindex =0; index <5; index++) {
? ? // 計(jì)算行號(hào)? 和? 列號(hào)
? ? ? ? introw = index / totalColumns;
? ? ? ? intcol = index % totalColumns;
? ? //根據(jù)行號(hào)和列號(hào)來(lái)確定 子控件的坐標(biāo)
? ? ? ? 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;
}