Masonry的使用

场景1:4个label等宽占满整个屏幕宽
//四个label等宽
{
UILabel *label1 = [[UILabel alloc] init];
label1.backgroundColor = [UIColor redColor];

UILabel *label2 = [[UILabel alloc] init];
label2.backgroundColor = [UIColor yellowColor];

UILabel *label3 = [[UILabel alloc] init];
label3.backgroundColor = [UIColor orangeColor];

UILabel *label4 = [[UILabel alloc] init];
label4.backgroundColor = [UIColor blueColor];

[self.view addSubview:label1];
[self.view addSubview:label2];
[self.view addSubview:label3];
[self.view addSubview:label4];

[label1 mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.view).offset(300);
    make.leading.equalTo(self.view);
    make.height.mas_equalTo(100);

// make.width.mas_equalTo(100);
}];

[label2 mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(label1.mas_right );
    make.height.equalTo(label1.mas_height);
    make.top.equalTo(label1.mas_top);
    make.width.equalTo(label1.mas_width);

// make.right.equalTo(self.view.mas_right);
}];

[label3 mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(label2.mas_right);
    make.height.equalTo(label2.mas_height);
    make.top.equalTo(label2.mas_top);
    make.width.equalTo(label2.mas_width);

// make.right.equalTo(self.view.mas_right);
}];

[label4 mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.equalTo(label3.mas_right);
    make.height.equalTo(label3.mas_height);
    make.top.equalTo(label3.mas_top);
    make.width.equalTo(label3.mas_width);
    make.right.equalTo(self.view.mas_right);
}];

}

效果图:
image.png

场景2:3个lable 1.2等宽 3是1.2宽的2倍 2高是1.3的2倍,宽占满整个屏幕
//三个控件,头2个等宽第三个是前面宽的2倍,第二个高是其他2个的2倍

{
UILabel *label1 = [[UILabel alloc] init];
label1.backgroundColor = [UIColor redColor];
UILabel *label2 = [[UILabel alloc] init];
label2.backgroundColor = [UIColor blueColor];
UILabel *label3 = [[UILabel alloc] init];
label3.backgroundColor = [UIColor grayColor];

[self.view addSubview:label1];
[self.view addSubview:label2];
[self.view addSubview:label3];

[label1 mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(self.view).offset(450);
    make.leading.equalTo(self.view);
    make.height.mas_equalTo(150);
}];

[label2 mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(label1.mas_top);
    make.left.equalTo(label1.mas_right);
    make.height.equalTo(label1.mas_height).multipliedBy(2);
    make.width.equalTo(label1.mas_width);
}];

[label3 mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(label2.mas_top);
    make.left.equalTo(label2.mas_right);
    make.height.equalTo(label2.mas_height).multipliedBy(0.5);
    make.width.equalTo(label2.mas_width).multipliedBy(2);
    make.right.equalTo(self.view.mas_right);
}];

}


Snip20190319_1.png
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • (一)Masonry介绍 Masonry是一个轻量级的布局框架 拥有自己的描述语法 采用更优雅的链式语法封装自动布...
    木易林1阅读 2,664评论 0赞 3
  • Masonry是一个轻量级的布局框架,拥有自己的描述语法,采用更优雅的链式语法封装自动布局,简洁明了并具有高可读性...
    3dcc6cf93bb5阅读 1,996评论 0赞 1
  • 因为之前开发时都是在xib文件中添加约束,或者代码中计算frame并没有接触过Masonry,现在写篇博客来归纳总...
    口子窖阅读 6,628评论 1赞 4
  • iOS_autoLayout_Masonry 概述 Masonry是一个轻量级的布局框架与更好的包装AutoLay...
    指尖的跳动阅读 1,381评论 1赞 4
  • 苹果自带的自动布局AutoLayout实现方式令人望而生畏,但还是包含着不错的思想,有人封装了第三方Masonry...
    顺其自然2017阅读 2,436评论 0赞 4

友情链接更多精彩内容