使用步骤:

  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基本用法的更多相关文章

  1. masonry 基本用法

    一:masonry 基本用法 fistView=[[UIView alloc] init]; fistView.backgroundColor=[UIColor redColor]; [self.vi ...

  2. Autolayout 第三方开源库

    转载自:http://blog.csdn.net/hmt20130412/article/details/46638625 今天才发现CSDN支持markdown了…还是给出新博客地址:Autolay ...

  3. 关于Masonry框架(AutoLayout)的用法--面向初学者

    Masonry作为目前较为流行的自动布局第三方框架,简单易用,大大减少了程序员花在UI布局和屏幕适配的精力与时间. 1 基本用法 1.1 事例1: 图1-1 // 首先是view1自动布局 [view ...

  4. Masonry和FDTemplateLayoutCell 结合使用示例Demo

    我们知道,界面布局可以用Storyboard或Xib结合Autolayout实现,如果用纯代码布局,比较热门的有Masonry.SDAutoLayout,下面的简单demo,采用纯代码布局,实现不定高 ...

  5. Masonry tableviewCell布局(转)

    转载自:http://www.henishuo.com/masonry-tableviewcell-layout/ 前言 说到iOS自动布局,有很多的解决办法.有的人使用xib/storyboard自 ...

  6. 【原】iOS学习之Masonry第三方约束

    1.Masonry概述 目前最流行的Autolayout第三方框架 用优雅的代码方式编写Autolayout 省去了苹果官方恶心的Autolayout代码 大大提高了开发效率 框架地址:https:/ ...

  7. Coding源码学习第四部分(Masonry介绍与使用(三))

    接上篇继续进行Masonry 的学习. (12)tableViewCell 布局 #import "TableViewController.h" #import "Tes ...

  8. iOS自动布局进阶用法

    本文主要介绍几个我遇到并总结的相对高级的用法(当然啦牛人会觉得这也不算什么). 简单的storyboard中上下左右约束,固定宽高啥的用法在这里就不做赘述了. autolayout自动布局是iOS6以 ...

  9. Masonry学习分享

    不完整目录 •UIScrollView 应用Masonry的正确用法 •tableHeaderView使用Masonry •同向文字显示优先级 1.基础篇 1.1基础使用 1.1.1运行效果 1.1. ...

随机推荐

  1. Flask--init和run启动研究---xunfeng巡风实例篇

    第一: 首先在view目录下的__init__.py文件定义好 (1) Flask实例 : app = Flask(__name__) (2) 数据库实例 Mongo = Conn.MongoDB(a ...

  2. POJ3246-Balanced Lineup,好经典的题,做法和HDU-I hate it 一样~~

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K   Case Time Limit: 2000MS Description For ...

  3. 虚拟机(Virtual Machine)和容器(Container)的对比

    目前云计算平台常用的虚拟化技术有虚拟机(Virtual Machine)和容器(Container)两种.虚拟机已经是比较成熟的技术,容器技术作为下一代虚拟化技术,国内的各厂商应用还不广,但似乎其代表 ...

  4. CSU 1605 数独

    题目大意: 9宫格每个位置都有对应的分数,填完数独后根据对应位置的分数相加之和求个最大值,不存在输出-1 说什么用位运算加速可以解决问题,但是对着标程还是T,最近学了dlx,发现这样解决数独快了很多 ...

  5. selenide01---截图

    1.junit:import com.codeborne.selenide.junit.ScreenShooter; @Rule public ScreenShooter makeScreenshot ...

  6. Open Judge 3339 List

    3339:List 总时间限制:  4000ms 内存限制:  65536kB 描述 写一个程序完成以下命令:new id ——新建一个指定编号为id的序列(id<10000)add id nu ...

  7. OC-Xcode中导入runtime框架,函数参数没有提示的处理方法

    在了解runtime时,如果自己编写runtime代码,需要先导入头文件: #import <objc/message.h> 之后,例如了解runtime的消息机制时,调用objc_msg ...

  8. 推荐10+必备的 WordPress 常用插件

    众多的WordPress插件,使得WordPress的功能得到了较大的扩展,但是也正是由于过多的插件,导致我们很难选择所需的插件.今天,倡萌就根据自己的经验,给WordPress新手推荐一些常用的插件 ...

  9. 从零开始写STL—模板元编程之any

    any class any; (since C++17) The class any describes a type-safe container for single values of any ...

  10. 佳能 imageclass mf40120

    加粉2612A 加粉方式: 完全拆解安装:在技术部 自已研究 简单拆开,一分为二,倒粉,然后,加分:一些其他单位 外部加粉,拆粉口外盖:一些其他单位