九宫格在iOS项目UI中是经常会出现的,在这里写了一个简单的for循环创建九宫格,供新手参考。
#pragma mark - createNineSquare
- (void)createNineSquare {
UIView *btnViewBackground = [[UIView alloc] initWithFrame:CGRectMake(0, bannerImageView.size.height, kDeviceWidth, 3*(kDeviceWidth-3)/4)];
btnViewBackground.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:btnViewBackground];
CGFloat btnMargin = 1; //间隔
CGFloat btnWidth = (kDeviceWidth-3)/4; //宽
CGFloat btnHeight = (kDeviceWidth-3)/4; //高
for (int i = 0; i<12; i++) {
int row = i/4; //行
int col = i%4; //列
btnView = [[UIView alloc] init];
btnView.frame = CGRectMake(col*(btnWidth + btnMargin), row*(btnHeight + btnMargin), btnWidth, btnHeight);
btnView.backgroundColor = [UIColor whiteColor];