iOS开发之Masonry(六)

本文介绍利用Masonry约束scrollview上的子控件,使得scrollView可以在约束的条件下滚动。核心代码如下:

// 创建scroll view
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    [self.view addSubview:scrollView];
    scrollView.backgroundColor = [UIColor lightGrayColor];
    
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0, 10, 10, 10));
    }];
    
    
    // 创建scroll view上的子控件
    // 第一个子控件
    UIView *containerView1 = [[UIView alloc] init];
    [scrollView addSubview:containerView1];
    containerView1.backgroundColor = [UIColor redColor];
    [containerView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.top.equalTo(scrollView);
        make.height.equalTo(@700);
        
        make.width.equalTo(scrollView);
        
    }];
    
    // 第二个子控件
    UIView *containerView2 = [[UIView alloc] init];
    [scrollView addSubview:containerView2];
    containerView2.backgroundColor = [UIColor blueColor];
    
    [containerView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(containerView1.mas_bottom);
        make.left.equalTo(scrollView);
        
        make.width.equalTo(scrollView);
        
        make.height.equalTo(@100);
        
    }];
    
    
    // 更新scroll view的约束,scroll view的底部等于其最后一个子控件的底部
    [scrollView mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(containerView2.mas_bottom);
        
    }];
注意点有:

1.scrollView上的子控件约束要完整。

2.当scrollView上的子控件更新时,要更新scrollView的约束。要使得scrollView的底部等于其最后子控件的底部。

3.注意使用约束方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值