iOS ui中的九宫格作图
九宫格作图
// 添加应用信息
// 1,总列数(一行最多三列)
int totalColumns = 3;
// 2,应用尺寸
CGFloat appW =85;
CGFloat appH = 90;
// 3,间隙= (控制器view的宽度 -3*应用宽度)/4
CGFloat marginX = (self.view.frame.size.width-totalColumns*appW)/(totalColumns+1);
CGFloat marginY = 20;
// 4,根据应用个数创建对应的框框
for (int index =0; index<self.apps.count; index++)
{
// 4.1,创建小框框
UIView *appView = [[UIView alloc]init];
// 设置背景色
appView.backgroundColor = [UIColor redColor];
// 4.2 计算框框的位置
// 计算行号和列号
int row = index / totalColumns;
int col = index % totalColumns;
CGFloat appX =marginX+col*(appW+marginX);
CGFloat appY = 30+row *(appH +marginY);
appView.frame = CGRectMake(appX, appY, appW, appH);
// 4.3添加框框到控制器
[self.view addSubview:appView];
}