AutoLayout三、九宫格
2015-07-22 23:30
453 查看
使用Masonry完成九宫格的约束设置,效果如图所示:
![](http://images0.cnblogs.com/blog2015/781566/201507/222323369902085.png)
首先里面的 每一项 又是一个单独的View, 外面套一个容器View
ItemView:
- (void)initSubviews { self.userInteractionEnabled = YES; _imageView.userInteractionEnabled = YES; _titleLabel.userInteractionEnabled = YES; @weakify(self); CGFloat width = _imageView.image.size.width; [_imageView mas_makeConstraints:^(MASConstraintMaker *make) { @strongify(self); make.top.mas_equalTo(self.mas_top);//图片贴顶部 make.centerX.mas_equalTo(self);//图片居中 make.width.mas_equalTo(width);//等于图片宽度 }]; [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { @strongify(self); make.top.mas_equalTo(self.imageView.mas_bottom).offset(20);//文字隔图片下20 make.centerX.mas_equalTo(self.imageView);//居中 make.bottom.mas_equalTo(self.mas_bottom);//文字底部贴superView }]; }
//九宫格布局约束核心代码 NSInteger Columns = 3; CGFloat ItemHeight = 80; CGFloat Gap = 35; __block UIView *lastView = nil; for (int i = 0; i < self.shareList.count; i++) { ZSYInviteFriendsItemView *item = _shareList[i]; [_shareView addSubview:item]; [item mas_makeConstraints:^(MASConstraintMaker *make) { if (lastView) { make.width.equalTo(lastView.mas_width); } else { make.size.mas_equalTo(CGSizeMake((SCREEN_WIDTH - Gap * (Columns + 1))/Columns, ItemHeight)); } if (i % Columns == 0) { make.left.equalTo(item.superview).offset(Gap); } else{ make.left.equalTo(lastView.mas_right).offset(Gap); } if (i % Columns == (Columns -1)) { make.right.equalTo(item.superview).offset(-Gap); } int top = (i / Columns) * Gap + ( i / Columns * ItemHeight); make.top.equalTo(item.superview).offset(top); lastView = item; }]; }
相关文章推荐
- ubuntu14.04 shell脚本用select报错
- QTP 默认场景恢复文件
- 微信朋友圈图片被强制替换 腾讯:服务器升级不稳定
- 简单五子棋服务器
- Codeforces Amr and Chemistry(数学+乱搞)
- 2015072201 - 越来越不像技术博客
- 沙盒获得根目录
- Upload your app to IonicView
- Nginx 笔记与总结(2)信号控制
- zabbix 的安装
- [原创] Zookeeper注册节点的掉线自动重新注册及测试方法
- C++析构函数,This指针,函数链
- 数据库中的悲观锁和乐观锁详解
- LeetCode Single Number II 单元素2
- com.opensymphony.xwork2.inject.DependencyException: com.opensymphony.xwork2.inject.ContainerImpl$Mis
- 用java实现 小孩围圈数三退一游戏
- 多重背包 HDU2191
- Tr A
- Shell练习-计算一下所有进程所占用内存大小的和
- 互联网营销、高管创业、一夜成名,“小米式”创业能改变世界?