int totalColumns = 3; CGFloat top=100;code
// 每一格的尺寸 CGFloat cellW = 100; CGFloat cellH = 100; // 間隙 CGFloat margin =(self.view.frame.size.width - totalColumns * cellW) / (totalColumns + 1); //根據格子個數建立對應的框框 for(int index = 0; index< 9; index++) { UIView *cellView = [[UIView alloc ]init ]; cellView.backgroundColor = [UIColor blueColor]; // 計算行號 和 列號 int row = index / totalColumns; int col = index % totalColumns; //根據行號和列號來肯定 子控件的座標 CGFloat cellX = margin + col * (cellW + margin); CGFloat cellY = row * (cellH + margin); cellView.frame = CGRectMake(cellX, cellY+top, cellW, cellH); // 添加到view 中 [self.view addSubview:cellView]; }