美文网首页
UIScrollView使用Masonry设置contentSi

UIScrollView使用Masonry设置contentSi

作者: 生命不止运动不息 | 来源:发表于2019-08-04 20:07 被阅读0次

    场景1:scrollview上有3个子视图,子视图高度确定,通过masonry设置scrollview的contentsize高度。

    方法:第一个子视图的顶部设置好与scrollview的顶部的值,最后一个子视图的底部设置好距离scrollview的底部的值,其他子视图正常设置。

    代码:

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        
        UIScrollView *scrollView = [UIScrollView new];
        [self.view addSubview:scrollView];
        [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view).mas_equalTo(0);
        }];
        
        UIView *v1 = [UIView new];
        [scrollView addSubview:v1];
        v1.backgroundColor = [UIColor redColor];
        [v1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(scrollView).mas_equalTo(0);
            make.centerX.equalTo(scrollView).mas_equalTo(0);
            make.width.mas_equalTo(300);
            make.height.mas_equalTo(200);
        }];
        
        UIView *v2 = [UIView new];
        [scrollView addSubview:v2];
        v2.backgroundColor = [UIColor orangeColor];
        [v2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(v1.mas_bottom).mas_equalTo(10);
            make.centerX.equalTo(scrollView).mas_equalTo(0);
            make.width.mas_equalTo(300);
            make.height.mas_equalTo(400);
        }];
        
        UIView *v3 = [UIView new];
        [scrollView addSubview:v3];
        v3.backgroundColor = [UIColor redColor];
        [v3 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(v2.mas_bottom).mas_equalTo(10);
            make.centerX.equalTo(scrollView).mas_equalTo(0);
            make.width.mas_equalTo(300);
            make.height.mas_equalTo(300);
            make.bottom.equalTo(scrollView).mas_equalTo(-50);
        }];
    }
    

    相关文章

      网友评论

          本文标题:UIScrollView使用Masonry设置contentSi

          本文链接:https://www.haomeiwen.com/subject/umoydctx.html