UIScrollView+Masonary实现自适应宽高并滚动

107 阅读1分钟

1、在Scrollview上增加一层containerView,作为其他子view容器。后续所有业务子view加在containerView上。

[self.view addSubview:self.contentScrollView];
[self.contentScrollView addSubview:self.containerView];
[self.containerView addSubview:self.confirmView];

2、设置containerView的bottom和rihgt对业务子View进行依赖

[self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
     make.left.right.equalTo(self.view);
     make.top.equalTo(self.contentScrollView);
     make.bottom.equalTo(self.confirmView).mas_offset(BottomHeight);  //(重点)
}];

3、使用Masonary完成布局后,设置ScrollView对containerView的bottom和rihgt进行依赖

// ScrollView布局
[self.contentScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
     make.left.right.bottom.equalTo(self.view);
     make.top.mas_offset(136);
 }];
// 设置ScrollView依赖(重点)
[self.contentScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
     make.bottom.equalTo(self.containerView);
}];

以此实现等同于设置SCrollView.contentSize的效果,不需要再设置contentSize即可滚动,且保留Masonary的自适应布局能力