九宫格 纯代码布局
用的时候修改kRows kCols KImgNumbers ,调用setupUI就可以 实现九宫格轻松布局
//间隙
#define kMargin 4
//屏幕宽
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
//屏幕高
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
//状态栏高度
#define kStatusHeight 20
//行
#define kRows 5
//列
#define kCols 3
//图片数
#define kImgNumbers 15
-(void)setupUI{
//imgView的宽高
//W
CGFloat imgW=(kScreenWidth-kMargin*(kCols + 1))/kCols;
//H
CGFloat imgH=(kScreenHeight-kStatusHeight -kMargin*(kRows +1))/kRows;
for (int i =0; i< kImgNumbers; i++) {
//行
NSInteger imgRow =i/kCols;
//列
NSInteger imgCol = i%kCols;
//X
CGFloat imgX =kMargin *(imgCol+1)+imgW *imgCol;
//Y
CGFloat imgY =kMargin *(imgRow +1)+imgH*imgRow +kStatusHeight;
UIImageView *imageView =[[UIImageView alloc]initWithFrame:CGRectMake(imgX, imgY, imgW, imgH)];
//背景色 方便查看
imageView.backgroundColor =[UIColor yellowColor];
[self.view addSubview:imageView];
}