如圖 ,創(chuàng)建九宮格
NSInteger row = 3; // 三列
NSInteger line = 3; // 三行
CGFloat buttonW = (SCREEN_WIDTH-40)/line;
CGFloat buttonH = buttonW;
CGFloat margin = 10;
for (int i = 0; i < 6; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(margin+i%row*(buttonW+margin), 200+i/line*(buttonH+margin), buttonW, buttonH);
button.backgroundColor = [UIColor redColor];
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
[self.view addSubview:button];
}