版权声明:本文为博主原创文章,未经博主允许不得转载。 https://cloud.tencent.com/developer/article/1336816
之前的一篇文章(https://www.jianshu.com/p/9563f5b646d9)讲过使用Masonry布局让Cell的高度实现自适应,其实这就是通过子视图撑起了父视图Cell的高度。今天再通过简单的代码来看下子视图如何通过Masonry的布局来撑起父视图的(让父视图的大小实现自适应)。
代码:
可复制代码:
UIView* superView = [UIViewallocinit]; self.viewaddSubview:superView; UIView* subView = [UIViewallocinit]; superViewaddSubview:subView; superView.backgroundColor = UIColor greenColor; subView.backgroundColor = UIColor yellowColor; superViewmas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.view.mas_left).offset(100); make.top.equalTo(self.view.mas_top).offset(100); }; subViewmas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(superView.mas_left).offset(10); make.top.equalTo(superView.mas_top).offset(10); make.right.equalTo(superView.mas_right).offset(-10); make.bottom.equalTo(superView.mas_bottom).offset(-10); make.width.height.equalTo(@(100)); };
效果图:
开发中应用:
效果图:
对于这样的高度根据文字内容变化的视图我们就能很好地实现了。
demo地址:https://gitee.com/liangsenliangsen/Masonry_subAndSup.git
本篇文章到这里就结束了,愿大家加班不多工资多,男同胞都有女朋友,女同胞都有男朋友。