批量创建按钮
- (void)viewDidLoad { [super viewDidLoad]; NSInteger total = 9; //数量 NSInteger btnWidth = 80;//按钮宽 NSInteger btnHeight = 30;//按钮高 NSInteger heightSpace = 15;//按钮行间距 NSInteger widthSpace = 15;//按钮列间距 NSInteger btnX = 30;//按钮X起点坐标 NSInteger btnY = 189;//按钮Y起点坐标 for (int i=0;i<total;i++) { UIButton *btns = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [btns setBackgroundColor:[UIColor whiteColor]]; [btns setTag:i+1];//设置按钮Tag [btns addTarget:self action:@selector(btnsAction:) forControlEvents:UIControlEventTouchUpInside]; CGRect frame; frame.origin.x = (i%3)*(btnWidth+widthSpace)+btnX; frame.origin.y = (i/3)*(btnHeight+heightSpace)+btnY; //frame.origin.x = btnX; // frame.origin.y = i*(btnHeight+heightSpace)+btnY; frame.size.width = btnWidth; frame.size.height = btnHeight; [btns setFrame:frame]; [self.view addSubview:btns]; } }
ios选择框
-(void)checkboxClick:(UIButton *)btn { btn.selected = !btn.selected; } - (void)viewDidLoad { UIButton *checkbox = [UIButton buttonWithType:UIButtonTypeCustom]; CGRect checkboxRect = CGRectMake(135,150,36,36); [checkbox setFrame:checkboxRect]; [checkbox setImage:[UIImage imageNamed:@"checkbox_off.png"] forState:UIControlStateNormal]; [checkbox setImage:[UIImage imageNamed:@"checkbox_on.png"] forState:UIControlStateSelected]; [checkbox addTarget:self action:@selector(checkboxClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:checkbox]; }