Skip to content

Instantly share code, notes, and snippets.

@tailang
Last active May 20, 2016 09:37
Show Gist options
  • Save tailang/e13e7c44b5031c26d780 to your computer and use it in GitHub Desktop.
Save tailang/e13e7c44b5031c26d780 to your computer and use it in GitHub Desktop.

Masonary与UIScrollView一起使用方案

 UIScrollView *scrollView = [[UIScrollView alloc] init];
 scrollView.userInteractionEnabled = YES;
 scrollView.scrollEnabled = YES;
 scrollView.backgroundColor = [UIColor greenColor];
 [self.view addSubview:scrollView];
 [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
     make.edges.equalTo(self.view);
 }];
 
 UIView *containView = [UIView new];
 containView.backgroundColor = [UIColor redColor];
 [scrollView addSubview:containView];
 [containView mas_makeConstraints:^(MASConstraintMaker *make) {
     make.edges.equalTo(scrollView);
     make.width.equalTo(scrollView);
     //如果可以左右滑动,正好是屏幕宽度的2倍
     //make.width.equalTo(scrollView).multipliedBy(2);
 }];
 
 
 UIView *subView = [UIView new];
 subView.backgroundColor = [UIColor blueColor];
 [containView addSubview:subView];
 
 [subView mas_makeConstraints:^(MASConstraintMaker *make) {
     make.left.equalTo(containView).with.offset(50);
     make.top.equalTo(containView).with.offset(self.view.height*1.2);
     make.size.mas_equalTo(CGSizeMake(50, 50));
 }];
 
 [containView mas_updateConstraints:^(MASConstraintMaker *make) {
     //这个约束,决定了containView的高,间接决定了scrollview的ContentSize的高
     //当subview比较靠上时,也有下拉上拉回弹效果
     make.bottom.equalTo(subView).with.offset(10).priority(999);
     make.height.greaterThanOrEqualTo(@(self.view.bounds.size.height+1)) 
 }];  
 
 ```  
 
 
 [exmple](https://github.com/SnapKit/Masonry/blob/master/Examples/Masonry%20iOS%20Examples/MASExampleScrollView.m)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment