2种九宫格

//双层for

//控制行数

for(inti = 0; i < 3; i ++) {

//控制列数

for(intj = 0; j < 3; j ++) {

//记录一下进入内层for循环的次数设置给按钮的标题

static int k = 0;

k ++;

//i和j控制行宽和列高

//80 + (10 + 30) * (j**////**i)相反布局会倒90°

UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(80 + (10 + 30) * j,80 +(10 + 30) * i, 30, 30)];

//btn.layer.contentsRect = 15;

[btn setTitle:[NSString stringWithFormat:@"%d",k] forState:UIControlStateNormal];

btn.backgroundColor= [UIColor orangeColor];

[self.view addSubview:btn];

}

}

}


//单层

//3行3列创建九宫格

for(inti = 0;i < 9 ; i ++) {

static int k = 0;

k ++;

//用提公因数的方法写x---(30(坐标点x)+i%3(多少行)*100(紧凑程度),y---(80(坐标点y)+i/3(多少列)*100(紧凑程度))

UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake((80 + i % 4 * 50), (120 + i / 4 * 50), 30, 30)];

btn.backgroundColor= [UIColor orangeColor];

[btn setTitle:[NSString stringWithFormat:@"%d",k] forState:UIControlStateNormal];

btn.layer.cornerRadius= 15;

[self.view addSubview:btn];

}

}

你可能感兴趣的:(2种九宫格)