Masonry学习之UIScrollView
来源:互联网 发布:hl线切割软件下载 编辑:程序博客网 时间:2024/06/05 08:25
这个例子比较简单,直接看布局的关键代码
- (id)init { self = [super init]; if (!self) return nil; UIScrollView *scrollView = UIScrollView.new; self.scrollView = scrollView; scrollView.backgroundColor = [UIColor grayColor]; [self addSubview:scrollView]; // scrollView在其父视图上的布局约束 [self.scrollView makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self); }]; [self generateContent]; return self;}- (void)generateContent { UIView* contentView = UIView.new; [self.scrollView addSubview:contentView]; // scrollView上有一个内容视图,其布局约束如下 [contentView makeConstraints:^(MASConstraintMaker *make) { make.edges.equalTo(self.scrollView); make.width.equalTo(self.scrollView); }]; UIView *lastView; CGFloat height = 25; for (int i = 0; i < 10; i++) { UIView *view = UIView.new; view.backgroundColor = [self randomColor]; [contentView addSubview:view]; UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)]; [view addGestureRecognizer:singleTap]; // 内容视图上的子视图约束 [view mas_makeConstraints:^(MASConstraintMaker *make) { // lastView为nil,当前视图的top为0,否则当前视图的 // top为lastView的bottom make.top.equalTo(lastView ? lastView.bottom : @0); // 当前视图的left为0 make.left.equalTo(@0); // 当前视图的宽度与内容视图的宽度相等 make.width.equalTo(contentView.width); // 当前视图的高度为height make.height.equalTo(@(height)); }]; height += 25; lastView = view; } [contentView makeConstraints:^(MASConstraintMaker *make) { // 内容视图的bottom为lastView的bottom make.bottom.equalTo(lastView.bottom); }];}
注意:
对于内容视图contentView,设置了如下几个约束:
make.edges.equalTo(self.scrollView);make.width.equalTo(self.scrollView);make.bottom.equalTo(lastView.bottom);
这与基本的视图约束是不同的,只因为它是滚动视图的子视图。因为滚动视图的内容是可以超过其自身边界的。
阅读全文
0 0
- Masonry学习之UIScrollView
- Masonry学习之UILabel
- Masonry学习之Array
- UIScrollView使用masonry布局
- Masonry学习之基本布局
- Masonry学习之更新约束
- Masonry学习之使用常量
- Masonry学习之复合edges
- Masonry学习之aspect fit
- Masonry学习之基本动画
- Masonry学习之链式属性
- Masonry学习之Distribute View
- Masonry学习之Layout Guides
- IOS学习之UIScrollView
- Masonry对UIScrollView添加约束
- iOS UIScrollView上使用Masonry
- iOS学习笔记之-UIScrollView
- iOS 自动布局框架学习之Masonry
- 160个破解练习之CrackMe 008
- Java基础之多态的详细解释
- 第8周 项目3- 对称矩阵压缩存储的实现与应用(2)
- Canvas绘制小球(面向对象思想)
- audio在ios微信浏览器中不支持自动播放问题
- Masonry学习之UIScrollView
- Spring的@Transactional事务处理详解
- DateUtil
- nested exception is java.sql.SQLException: 索引中丢失 IN 或 OUT 参数::X
- java soket编程
- JAVA API文档
- 基于深度学习的风格迁移转换的两种实现——style-transfer和fast-neural-style-tensorflow
- 第九周 【项目1
- Java并发编程札记-(三)JUC原子类-06JDK1.8新增:LongAdder、DoubleAdder、LongAccumulator、DoubleAccumulator