for循環(huán)創(chuàng)建UIbutton和UIimageview等控件

@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;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容