IOS开发控件视图day11:九宫格坐标排列
程序员文章站
2022-07-11 13:46:16
//1、创建每一个UIViewUIView *appView = [[UIView alloc]init];//2、设置appView的属性...
//假设每行的应用个数
int colums = 5;
CGFloat viewWidth = self.view.frame.size.width;//当前页面的宽度
//每个应用的宽和高,固定值
CGFloat appW = 60;
CGFloat appH = 90;
CGFloat marginTop = 30;//第一行距离顶部的距离
CGFloat marginX = (viewWidth - colums * appW)/(colums +1);//每一行第一个应用距离左边的距离
CGFloat marginY = marginX;//假设每行之间的间距与marginX相等
for (int i = 0; i< 20; i ++) {
UIView *appView = [[UIView alloc]init];//创建每个应用(UIView)
appView.backgroundColor = [UIColor blueColor]; //背景色
//计算每个单元格所在的类的索引
int colIdx = i % colums;
//计算每个行所在的行索引
int rowIdx = i / colums;
//动态设置frame属性
CGFloat appX = marginX +colIdx *(appW + marginX);//X坐标
CGFloat appY = marginTop +rowIdx *(appH +marginY);//Y坐标
appView.frame = CGRectMake(appX, appY, appW, appH);
[self.view addSubview:appView]; //添加到视图控制器
}
效果图:
本文地址:https://blog.csdn.net/wenyu_Saitama/article/details/107507725