Masonry的使用之利用子视图撑起父视图

之前的一篇文章(https://www.jianshu.com/p/9563f5b646d9)讲过使用Masonry布局让Cell的高度实现自适应,其实这就是通过子视图撑起了父视图Cell的高度。今天再通过简单的代码来看下子视图如何通过Masonry的布局来撑起父视图的(让父视图的大小实现自适应)。

代码:

子视图撑起父视图

可复制代码:

UIView* superView = [[UIViewalloc]init];

    [self.viewaddSubview:superView];

   UIView* subView = [[UIViewalloc]init];

    [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

本篇文章到这里就结束了,愿大家加班不多工资多,男同胞都有女朋友,女同胞都有男朋友。😊

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容