九宫格布局的思路

思路分析:

1.布局View

这里为了简化代码,只写一点核心的思想

//总共有3行

int totalCol = 3;

CGFloat viewW = 80;

//定义的view的宽和高

CGFloat viewH = 90;

//宽度的间距

CGFloat marginX = (Self.view.bounds.size.width- viewW*totalCol)/(totalCol+1);

CGFloat marginY = 10;

//计算行数和列数

for (int i =0;i<self.appList.count;i++)

{

int row = i/totalCol;//行数是除

int col = i%totalCol;//列数是取摸

CGFloat x = marginX+(ViewW+marginX)*col;//根据行数,列数计算好X,Y的值后

CGFloat y = marignY+(viewH+marginY)*row;

UIView *appView = [[UIView alloc]initwithframe:CGRectMake(CGFloatX,CGFloatY,viewW,viewH)];

[self.view addsubview:app view];

//创建appview 内部的细节

//1.UIImageView  显示图标

UIImageView  *imageView  = [[UIImageView alloc]initwithframe:CGRectMake(0,0,viewW,50)];//xy是距离父视图的距离

imageView.backgroundcolor = [UIColor redcolor];

[appview addsubview:imageView];//添加到父视图中

//2.UILabel           显示名称

UILabel *label = [[UILabel alloc]initwithframe:CGRectMake(0,imageView.bounds.size.height,viewW,20)];

label.background.color = [UIColor greencolor];

label.text = dict[@”name”];

label.font = [UIFont systemfontwithsize:12];//设置系统字体12号

label/textAlignment =  NSTextAlignmentCcneter;//设置文字居中显示

[appview addsubview:label];//添加到父视图中

//3.UIButton          点击按钮

//UIButton alloc init == UIButton buttonwithtype:UIButtontypeCustom等价

UIButton *button =[UIButton buttonwithtype:UIButtontypeCustom];

[button settitle :@”正在下载” forstate:UIControlStateNormal];

button.tillable.font =  [UIFont systemfontofsize:12];

}

这里需要注意的是:不能使用如下代码直接设置button的title 需要指明状态

button.titlelabel.text = @”下载”;

[button settle:@”下载” forstate:UIControllerstateNomal];

设置Button的背景图片也不能这样设置

button.imageview.image = [UiImageview image named:@”love.jpg”];

而是需要这样设置

[button setbackgroundimage:UIImage image named:@”love.jpg”  forstate:UIControlStateNomal];

    原文作者:九宫格问题
    原文地址: https://blog.csdn.net/Ronaldo_Carry/article/details/48786177
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞