当前位置:   article > 正文

UIScrollView+Masonry解决添加带ScrollView上的view不显示_ios collectionview 添加到scrollview上不显示

ios collectionview 添加到scrollview上不显示

以前遇到过这关问题查了一下资料解决了就没在意,今天又出现这个问题做下记录加深记忆,也给大家分享一下。

问题:

我们使用Autolayout来布局UIScrollVie,添加的view不显示,打印log会看到view的宽和高是0。比如:

  1. __weak typeof(self) weak_self = self;
  2. [self.mainScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
  3. make.edges.equalTo(weak_self).insets(UIEdgeInsetsMake(0, 0, buyingViewHeight, 0));
  4. }];<pre name="code" class="objc">[self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
  5. make.left.top.right.equalTo(self.mainScrollView);
  6. make.height.equalTo(@60);
  7. }];

 
UIScrollView的leading/trailing/top/bottom是相对于自己的ContentSize而不是Bounds来确定的。而ContentSize又是根据子视图决定的。 

上面的代码产生的结果就是headerView的高和宽都是0。因为mainScrollView的四边都是依据ContentSize,这个时候ContentSize又不确定,这样就导致了ScrollView的子视图不显示,子视图上面的点击事件不相应等问题。

解决:

既然我们直接添加到ScrollView的view不能使用它的约束条件,我们就抛开他重新创建一个view直接覆盖到ScrollView上,这个view的宽和高等于ContentSize的width和height:
  1. __weak typeof(self) weak_self = self;
  2. [self.mainScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
  3. make.edges.equalTo(weak_self).insets(UIEdgeInsetsMake(0, 0, 0, 0));
  4. }];
  5. [self.container mas_makeConstraints:^(MASConstraintMaker *make) {
  6. make.edges.equalTo(self.mainScrollView);
  7. make.width.height.equalTo(self.mainScrollView);
  8. }];
  9. [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
  10. make.left.top.right.equalTo(self.container);
  11. make.height.equalTo(@60);
  12. }];
大家有疑问可以评论,一起学习,共勉之!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/529902
推荐阅读
相关标签
  

闽ICP备14008679号