@interface ViewController () @property (nonatomic,strong) NSArray *apps; //获取.plist数据 @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //九宫格的总列数 int totalColumns=5; //1.1个格子的尺寸 CGFloat appW=50; CGFloat appH=60; //2.计算间隙 =(控制器view的宽度 -5*应用宽度)/应用宽度+1 CGFloat margin=(self.view.frame.size.width-totalColumns*appW)/(totalColumns+1); //3.要的应用个数创建对应的格子 for (int index=0; index<self.apps.count; index++) { //3.1 创建1个格子 UIView *appView=[[UIView alloc]init]; //设置背景色 appView.backgroundColor=[UIColor redColor]; //3.2计算行号和列号 int row =index /totalColumns; int col =index % totalColumns; CGFloat appX =margin+ col*(appW + margin); CGFloat appY =30+ row*(appH + margin); appView.frame=CGRectMake(appX, appY, appW, appH); //3.3添加格子到控制器的View [self.view addSubview:appView]; } }
时间: 2024-10-19 00:51:25