Masonry scrollview循环布局
2016-03-31 00:33
183 查看
前言
说到iOS自动布局,有很多的解决办法。有的人使用
xib/storyboard自动布局,也有人使用
frame来适配。对于前者,笔者并不喜欢,也不支持。对于后者,更是麻烦,到处计算高度、宽度等,千万大量代码的冗余,对维护和开发的效率都很低。
笔者在这里介绍纯代码自动布局的第三方库:
Masonry。这个库使用率相当高,在全世界都有大量的开发者在使用,其
star数量也是相当高的。
效果图
本节详解Masonry的循环创建视图的基本用法,先看看效果图:
核心代码
看下代码:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | @interface ScrollViewController () @property (nonatomic, strong) UIScrollView *scrollView; @end @implementation ScrollViewController - (void)viewDidLoad { [super viewDidLoad]; self.scrollView = [[UIScrollView alloc] init]; self.scrollView.pagingEnabled = NO; [self.view addSubview:self.scrollView]; self.scrollView.backgroundColor = [UIColor lightGrayColor]; CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width; UILabel *lastLabel = nil; for (NSUInteger i = 0; i < 20; ++i) { UILabel *label = [[UILabel alloc] init]; label.numberOfLines = 0; label.layer.borderColor = [UIColor greenColor].CGColor; label.layer.borderWidth = 2.0; label.text = [self randomText]; // We must preferredMaxLayoutWidth property for adapting to iOS6.0 label.preferredMaxLayoutWidth = screenWidth - 30; label.textAlignment = NSTextAlignmentLeft; label.textColor = [self randomColor]; [self.scrollView addSubview:label]; [label mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(15); make.right.mas_equalTo(self.view).offset(-15); if (lastLabel) { make.top.mas_equalTo(lastLabel.mas_bottom).offset(20); } else { make.top.mas_equalTo(self.scrollView).offset(20); } }]; lastLabel = label; } [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.mas_equalTo(self.view); // 让scrollview的contentSize随着内容的增多而变化 make.bottom.mas_equalTo(lastLabel.mas_bottom).offset(20); }]; } - (UIColor *)randomColor { CGFloat hue = ( arc4random() % 256 / 256.0 ); // 0.0 to 1.0 CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from white CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0, away from black return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1]; } - (NSString *)randomText { CGFloat length = arc4random() % 50 + 5; NSMutableString *str = [[NSMutableString alloc] init]; for (NSUInteger i = 0; i < length; ++i) { [str appendString:@"测试数据很长,"]; } return str; } @end |
讲解
对于循环创建,我们需要记录下一个视图所依赖的控件,这里使用了lastLabel来记录。
我们要想让
scrollview的
contentSize随内容的变化而变化,那么就我们一定要添加注意添加约束:
1 2 3 4 5 6 7 8 | [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.mas_equalTo(self.view); // 让scrollview的contentSize随着内容的增多而变化 make.bottom.mas_equalTo(lastLabel.mas_bottom).offset(20); }]; |
scrollview和
tableview,我们不能使用
bottom来计算其高,因为这个属性对于
scrollview和
tableview来说,不用用来计算高度的,而是用于计算
contentSize.height的。我们要想随内容而变化,以便可滚动查看,就必须设置
bottom约束。
相关文章推荐
- Qt浅谈之四十七下拉列表菜单
- Xcode快捷键
- c++笔记(3.30)
- 创业的第一百五十六天
- Masonry tableviewCell布局
- 3.27PSP及体会
- 设计模式学习笔记——命令模式
- spring(1)
- Masonry 比例(multipliedBy)
- 【8】JAVA---地址App小软件(AddrDaoFile .class)(数据层)
- 【8】JAVA---地址App小软件(AddrDaoFile .class)(数据层)
- Masonry自动布局:复合约束
- 随机生成题目的代码分析
- 睡觉前请关灯的 破解尝试版本 由已知解求一个矩阵的步骤
- Masonry整体动画更新约束
- 课本学习笔记4:第三章 20135115臧文君
- 作业2016
- uva 10911 Forming Quiz Teams
- 【SSH整合异常】启动Tomecat报错
- Raspbian安装mysql失败问题