Masonry基本用法
使用步骤:
1.导入框架
2.导入头文件,或者直接导入.pch文件中
//省略前缀 'max_'的宏:
#define MAS_SHORTHAND // 自动装箱:自动把基本数据类型转化成对象,int => NSNumber
#define MAS_SHORTHAND_GLOBALS #import "Masonry.h"
3.实例1>:假设有个红色的View,居中显示,尺寸100.效果图:

UIView *redV = [[UIView alloc]init];
redV.backgroundColor = [UIColor redColor];
[self.view addSubview:redV]; // 设置布局
[redV makeConstraints:^(MASConstraintMaker *make) {
// 在这里设置布局,描述make,这个make就表示红色所有的布局约束
// 约束make == 约束redV // self.view 表示在self.view中居中
make.center.equalTo(self.view); make.size.equalTo(CGSizeMake(, ));
}];
实例2>:假设有个红色的View,上下左右有个20的间距. 效果图:

实现该效果有三种方法:
第一种:分别对redView的上左下右进行约束
UIView *redV = [[UIView alloc]init];
redV.backgroundColor = [UIColor redColor];
[self.view addSubview:redV]; // 设置布局
[redV makeConstraints:^(MASConstraintMaker *make) {
// 上
make.top.equalTo(self.view).offset();
// 下
make.bottom.equalTo(self.view).offset(-);
// 左
make.left.equalTo(self.view).offset();
// 右
make.right.equalTo(self.view).offset(-);
}];
第二种:合并约束条件相同的属性:
[redV makeConstraints:^(MASConstraintMaker *make) {// 上左
make.top.left.equalTo(self.view).offset();
// 下右
make.right.bottom.equalTo(self.view).offset(-);
}];
第三种,使用内边距结构体:
[redV makeConstraints:^(MASConstraintMaker *make) {
UIEdgeInsets inset = UIEdgeInsetsMake(, , , );
make.edges.equalTo(inset);
}];
实例3>参照兄弟view

// 蓝色blueV
UIView *blueV = [[UIView alloc]init];
blueV.backgroundColor = [UIColor blueColor];
[self.view addSubview:blueV]; // 设置约束蓝色blueV,左,上,右20,高50
[blueV makeConstraints:^(MASConstraintMaker *make) {
make.top.left.equalTo(self.view).offset();
make.right.equalTo(self.view).offset(-);
make.height.equalTo();
}]; // 红色redV
UIView *redV = [[UIView alloc]init];
redV.backgroundColor = [UIColor redColor];
[self.view addSubview:redV]; // 设置红色view,约束:宽度 = 蓝色*0.5 ,高度相等,right= right,顶部20的间距
[redV makeConstraints:^(MASConstraintMaker *make) {
make.width.equalTo(blueV).multipliedBy(0.5);
make.right.height.equalTo(blueV);
make.top.equalTo(blueV.bottom).offset();
}];
前言
说到iOS自动布局,有很多的解决办法。有的人使用xib/storyboard自动布局,也有人使用frame来适配。对于前者,笔者并不喜欢,也不支持。对于后者,更是麻烦,到处计算高度、宽度等,千万大量代码的冗余,对维护和开发的效率都很低。
笔者在这里介绍纯代码自动布局的第三方库:Masonry。这个库使用率相当高,在全世界都有大量的开发者在使用,其star数量也是相当高的。
效果图
本节详解Masonry的基本用法,先看看效果图:

我们这里要布局使这三个控件的高度一致,而绿色和红色的控件高度和宽度相待。
核心代码
看下代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
- (void)viewDidLoad {
[super viewDidLoad];
UIView *greenView = UIView.new;
greenView.backgroundColor = UIColor.greenColor;
greenView.layer.borderColor = UIColor.blackColor.CGColor;
greenView.layer.borderWidth = 2;
[self.view addSubview:greenView];
UIView *redView = UIView.new;
redView.backgroundColor = UIColor.redColor;
redView.layer.borderColor = UIColor.blackColor.CGColor;
redView.layer.borderWidth = 2;
[self.view addSubview:redView];
UIView *blueView = UIView.new;
blueView.backgroundColor = UIColor.blueColor;
blueView.layer.borderColor = UIColor.blackColor.CGColor;
blueView.layer.borderWidth = 2;
[self.view addSubview:blueView];
// 使这三个控件等高
CGFloat padding = 10;
[greenView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(padding);
make.left.mas_equalTo(padding);
make.right.mas_equalTo(redView.mas_left).offset(-padding);
make.bottom.mas_equalTo(blueView.mas_top).offset(-padding);
// 三个控件等高
make.height.mas_equalTo(@[redView, blueView]);
// 红、绿这两个控件等高
make.width.mas_equalTo(redView);
}];
[redView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.height.bottom.mas_equalTo(greenView);
make.right.mas_equalTo(-padding);
make.left.mas_equalTo(greenView.mas_right).offset(padding);
}];
[blueView mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(greenView);
make.bottom.mas_equalTo(-padding);
make.left.mas_equalTo(padding);
make.right.mas_equalTo(-padding);
}];
}
|
讲解
添加约束的方法是:mas_makeConstraints。我们可以看到,约束可以使用链式方式,使用方法很简单,看起来像一句话。
看这句话:make.top.height.bottom.mas_equalTo(greenView),意思是:使我的顶部、高度和底部都与greenView的顶部、高度和底部相等。因此,只要greenView的约束添加好了,那么redView的顶部、高度和底部也就自动计算出来了。
大多时候,我们并不会将一句话完整地写出来,而是使用简写的方式。如:
|
1
2
3
|
make.right.mas_equalTo(-padding);
|
其完整写法为:
|
1
2
3
|
make.right.mas_equalTo(bludView.superView.mas_right).offset(-padding);
|
当我们是要与父控件相对约束时,可以省略掉父视图。注意,并不是什么时候都可以省略,只有约束是同样的才可以省略。比如,约束都是right才可以。如果是一个left一个是right,那么就不能省略了。
Masonry基本用法的更多相关文章
- masonry 基本用法
一:masonry 基本用法 fistView=[[UIView alloc] init]; fistView.backgroundColor=[UIColor redColor]; [self.vi ...
- Autolayout 第三方开源库
转载自:http://blog.csdn.net/hmt20130412/article/details/46638625 今天才发现CSDN支持markdown了…还是给出新博客地址:Autolay ...
- 关于Masonry框架(AutoLayout)的用法--面向初学者
Masonry作为目前较为流行的自动布局第三方框架,简单易用,大大减少了程序员花在UI布局和屏幕适配的精力与时间. 1 基本用法 1.1 事例1: 图1-1 // 首先是view1自动布局 [view ...
- Masonry和FDTemplateLayoutCell 结合使用示例Demo
我们知道,界面布局可以用Storyboard或Xib结合Autolayout实现,如果用纯代码布局,比较热门的有Masonry.SDAutoLayout,下面的简单demo,采用纯代码布局,实现不定高 ...
- Masonry tableviewCell布局(转)
转载自:http://www.henishuo.com/masonry-tableviewcell-layout/ 前言 说到iOS自动布局,有很多的解决办法.有的人使用xib/storyboard自 ...
- 【原】iOS学习之Masonry第三方约束
1.Masonry概述 目前最流行的Autolayout第三方框架 用优雅的代码方式编写Autolayout 省去了苹果官方恶心的Autolayout代码 大大提高了开发效率 框架地址:https:/ ...
- Coding源码学习第四部分(Masonry介绍与使用(三))
接上篇继续进行Masonry 的学习. (12)tableViewCell 布局 #import "TableViewController.h" #import "Tes ...
- iOS自动布局进阶用法
本文主要介绍几个我遇到并总结的相对高级的用法(当然啦牛人会觉得这也不算什么). 简单的storyboard中上下左右约束,固定宽高啥的用法在这里就不做赘述了. autolayout自动布局是iOS6以 ...
- Masonry学习分享
不完整目录 •UIScrollView 应用Masonry的正确用法 •tableHeaderView使用Masonry •同向文字显示优先级 1.基础篇 1.1基础使用 1.1.1运行效果 1.1. ...
随机推荐
- laravel groupby分组问题。
laravel 5.7使用groupBy分组查询时会提示一个错误,但是sql可以执行. 因为:mysql从5.7以后,默认开启了严格模式. 解决方法:将/config/database.php 中:关 ...
- UVA10673 上下界问题
#include <iostream> #include<cstdio> using namespace std; #define LL long long LL a,b,m, ...
- D 题
题目大意:找朋友,最好把朋友最多的一堆的人数输出 运用并查集,每次更新最大数即可: 代码: #include <iostream> #include <cstdio> #inc ...
- 【bzoj4260】 Codechef REBXOR trie树
Input 输入数据的第一行包含一个整数N,表示数组中的元素个数. 第二行包含N个整数A1,A2,…,AN. Output 输出一行包含给定表达式可能的最大值. Sample Input ...
- 【BZOJ1031】字符加密Cipher(后缀数组)
题意:将一个长度为2n(复制粘贴后)的字符串的所有长度为n的后缀从小到大排序,并依次输出它们的最后一个字母. n<=100000 思路:裸SA,模板真难背 P党不得不写成C++风格 ..]of ...
- 清北省选 DAY last 集锦
这是题目描述的链接: http://lifecraft-mc.com/wp-content/uploads/2018/03/problems1.pdf (虽然这次没去清北,但还是厚颜无耻的做了一下这套 ...
- 2017多校Round2(hdu6045~hdu6055)
补题进度:10/11 1001(不等式) 根据题意列不等式,解一解就行了 1002(套路) 题意: 给定一个随机产生的1e6*1e6的矩阵和一个1e3*1e3的矩阵,你要回答这个1e3*1e3的小矩阵 ...
- Android 学习路线图(转载自https://blog.csdn.net/lixuce1234/article/details/77947405)
程序设计 一.java (a)基本语法(如继承.异常.引用.泛型等) Java核心技术 卷I(适合入门) 进阶 Effective Java中文版(如何写好的Java代码) Java解惑 (介绍烂Ja ...
- PLSQL安装资料
一.plsql developer 注册码 plsql developer 10 注册码 product code :4v6hkjs66vc944tp74p3e7t4gs6duq4m4szbf3t38 ...
- spring配置文件加密
原文:http://www.open-open.com/code/view/1453520072183 spring框架在一些对安全性要求较高的生产环境下,配置文件不允许出现明文用户名密码配置,如数据 ...