九宫格布局的思路

思路分析:

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];


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值