//宏定义屏幕宽高
#define ScreenW self.view.frame.size.width
#define ScreenH self.view.frame.size.Height
//设置控件的宽高
CGFloat appsW = 80;
CGFloat appsH = 100;
//设置控件的列数
int columnCount = 3;
//设置控件的顶边间距
CGFloat topMarginSpacing = 75;
//设置控件列间距
CGFloat colMarginSpacing = (ScreenW - (appsW * columnCount)) / (columnCount + 1);
//设置控件的行间距
CGFloat rowMarginSpacing = colMarginSpacing;
//遍历整个数组创建控件
for (int i = 0; i < self.apps.count; i ++) {
UIView *appsView = [[UIView alloc] init];
appsView.backgroundColor = [UIColor redColor];
//计算列索引
int indexColumn = i % columnCount;
//计算行索引
int indexRow = i / columnCount;
//计算appsView的x、y
CGFloat appsX = indexColumn * (appsW + colMarginSpacing) + colMarginSpacing;
CGFloat appsY = indexRow * (appsH + rowMarginSpacing) + topMarginSpacing;
//设置appsView的frame
appsView.frame = CGRectMake(appsX, appsY, appsW, appsH);
//添加appsView到父控件view上
[self.view addSubview:appsView];
}
欢迎大家留言评论!