Masonry自适应高度


项目里经常会需要根据子视图的高度自动改变父视图的高度。

简单举个例子,绿色区域作为父视图,红色区域添加在绿色区域上。
现在让绿色区域的高度跟着红色区域的高度来自动改变。

代码如下


	UIView *view = [[UIView alloc]init];
    view.backgroundColor = UIColor.greenColor;
    [self.view addSubview:view];
    contentView *content =[[contentView alloc]init];
    content.backgroundColor = UIColor.redColor;
    [view addSubview:content];
    
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(@100);
        make.right.equalTo(@-100);
    }];
    float contentHeight = [content setHeight];
    [content mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(@50);
        make.right.equalTo(@-100);
        make.height.mas_equalTo(contentHeight);
        make.bottom.equalTo(view.mas_bottom).mas_offset(@-10);
    }];

思路:绿色区域view不设置高度。红色区域 设置高度,并设置底部距离view的底间距

  make.bottom.equalTo(view.mas_bottom).mas_offset(@-10);


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM