Masonry是一个轻量级的布局框架 拥有自己的描述语法 采用更优雅的链式语法封装自动布局 简洁明了 并具有高可读性。比我们使用自动布局,繁琐的约束条件,好用多了。下面我们来学学masonry的使用方法。

首先我们要下载Masonry源码。源码地址如下:

https://github.com/Masonry/Masonry

将源码下载下来后,可以直接编译过使用的。

然后将源码中的Masonry和Headers目录下的文件拷贝到我们的工程中,并添加进项目中。并在项目的编译设置中的header search paths 选项中增加$(SRCROOT)/Headers 和$(SRCROOT)/Masonry

然后在我们的项目预编译文件中增加

#import "Masonry.h"

然后我们就可以使用masonry的语句来设置我们的界面了。下面我们开始吧。

Masonry 支持的常用属性如下:

@property (nonatomic, strong, readonly) MASConstraint *left;

@property (nonatomic, strong, readonly) MASConstraint *top;

@property (nonatomic, strong, readonly) MASConstraint *right;

@property (nonatomic, strong, readonly) MASConstraint *bottom;

@property (nonatomic, strong, readonly) MASConstraint *leading;

@property (nonatomic, strong, readonly) MASConstraint *trailing;

@property (nonatomic, strong, readonly) MASConstraint *width;

@property (nonatomic, strong, readonly) MASConstraint *height;

@property (nonatomic, strong, readonly) MASConstraint *centerX;

@property (nonatomic, strong, readonly) MASConstraint *centerY;

@property (nonatomic, strong, readonly) MASConstraint *baseline;

·       首先在Masonry中能够添加autolayout约束有三个函数

  • - (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block;

  • - (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker *make))block;

  • - (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker *make))block;

  • mas_makeConstraints 只负责新增约束 Autolayout不能同时存在两条针对于同一对象的约束 否则会报错

  • mas_updateConstraints 针对上面的情况 会更新在block中出现的约束 不会导致出现两个相同约束的情况

  • mas_remakeConstraints 则会清除之前的所有约束 仅保留最新的约束

  • 三种函数善加利用 就可以应对各种情况了

一个小例子:

//从此以后基本可以抛弃CGRectMake了

UIView *sv = [UIView new];

//在做autoLayout之前 一定要先将view添加到superview上 否则会报错

[self.view addSubview:sv];

//mas_makeConstraints就是Masonry的autolayout添加函数 将所需的约束添加到block中行了

[sv mas_makeConstraints:^(MASConstraintMaker *make) {

//将sv居中(很容易理解吧?)

make.center.equalTo(ws.view);

//将size设置成(300,300)

make.size.mas_equalTo(CGSizeMake(300, 300));

}];

equalTo 和 mas_equalTo的区别在哪里呢? 其实 mas_equalTo是一个MACRO,比较的是值,equalTo比较的是view。

小例子二:

UIView *sv1 = [UIView new];

sv1.backgroundColor = [UIColor redColor];

[sv addSubview:sv1];

[sv1 mas_makeConstraints:^(MASConstraintMaker *make) {

make.edges.equalTo(sv).with.insets(UIEdgeInsetsMake(10, 10, 10, 10));

}];

UIView *sv2 = [UIView new];

UIView *sv3 = [UIView new];

sv2.backgroundColor = [UIColor greenColor];

sv3.backgroundColor = [UIColor blueColor];

[sv addSubview:sv2];

[sv addSubview:sv3];

int padding1 = 10;

[sv2 mas_makeConstraints:^(MASConstraintMaker *make) {

make.centerY.mas_equalTo(sv.mas_centerY);

make.left.equalTo(sv.mas_left).with.offset(padding1);

make.right.equalTo(sv3.mas_left).with.offset(-padding1);

make.height.mas_equalTo(@150);

make.width.equalTo(sv3);

}];

[sv3 mas_makeConstraints:^(MASConstraintMaker *make) {

make.centerY.mas_equalTo(sv.mas_centerY);

make.left.equalTo(sv2.mas_right).with.offset(padding1);

make.right.equalTo(sv.mas_right).with.offset(-padding1);

make.height.mas_equalTo(@150);

make.width.equalTo(sv2);

}];

例子三:

[self.mobileView mas_makeConstraints:^(MASConstraintMaker *make){

make.left.equalTo(self.view.mas_left).with.offset(0);

make.right.equalTo(self.view.mas_right).with.offset(0);

make.top.equalTo(self.view.mas_top).with.offset(MobileView_To_Top);

make.height.mas_equalTo(@Height_of_MobileView);

make.width.equalTo(self.view);

}];

[self.tipLabel mas_makeConstraints:^(MASConstraintMaker *make){

make.left.equalTo(self.view.mas_left).with.offset(0);

make.right.equalTo(self.view.mas_right).with.offset(0);

make.top.equalTo(self.view.mas_top).with.offset(Tiplabel_To_Top);

make.height.mas_equalTo(@Height_of_TipLabel);

make.width.equalTo(self.view);

}];

[self.nextButton mas_makeConstraints:^(MASConstraintMaker *make){

make.left.equalTo(self.view.mas_left).with.offset(NextButton_To_Left);

make.right.equalTo(self.view.mas_right).with.offset(-NextButton_To_Left);

make.top.equalTo(self.view.mas_top).with.offset(NextButton_To_Top);

make.height.mas_equalTo(@Height_of_NextButton);

}];

注意在xib或者storyboard中使用masonry框架相关方法的时候要将use Auto layout选项去掉,否则会不起作用。

Masonry布局框架的使用的更多相关文章

  1. IOS控件布局之Masonry布局框架

    前言: 回想起2013年做iOS开发的时候,那时候并没有采用手写布局代码的方式,而是采用xib文件来编写,如果使用纯代码方式是基于window的size(320,480)计算出一个相对位置进行布局,那 ...

  2. Masonry 轻量级布局框架的使用

    iOS 提供了自动布局的方法,但是原生的方法使用太过麻烦 ,Masonry 框架提供了类似的方法,同样可以实现自动布局 ,代码更加直观,而且容易理解. Masonry 是一个轻量级的布局框架.拥有自己 ...

  3. Masonry 布局 cell 高度适应的一种方案(实现类似朋友圈简单布局)

    来源:伯乐在线 - 夏天然后 链接:http://ios.jobbole.com/89298/ 点击 → 申请加入伯乐在线专栏作者 前言: 我模仿的是微博的布局所以也就没有 评论动态刷新cell. 1 ...

  4. ios7.1 masonry布局中出现的问题

    UITextView中如果添加了子空间,比如自己添加一个placeHolder,placeHolder的Label使用masonry布局,会出现崩溃,原因是:在UITextView没有对子控件进行布局 ...

  5. iOS下的界面布局利器-MyLayout布局框架

      Swift:TangramKit: https://github.com/youngsoft/TangramKit OC:MyLayout: https://github.com/youngsof ...

  6. LayoutSimple简易响应式CSS布局框架

    开发这个css布局的目的是为了少做一些重复的工作,一是前端或多或少会开发一些很小的响应式项目, 二是UI设计的出来的界面总是各种布局各种样式,这个时候如果前端去使用Bootstrap或者Foundat ...

  7. iOS masonry布局在iOS11/12上正常 iOS9/10却异常

    使用masonry布局,可以布局一套,适配所有机型,但是有时候会出现一些比较特殊的情况,每次iOS11上面开发,开发完成之后,在iOS9,iOS10上查看的时候发现布局与iOS11不完全一致,有的高度 ...

  8. [Swift通天遁地]六、智能布局-(8)布局框架的使用:多分辨率适配和横竖屏布局

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  9. 响应式布局框架 Pure-CSS 5.0 示例中文版-上

    0. Pure-CSS 介绍 Pure CSS 是雅虎出品的 CSS 框架, 依托于Normalize.CSS,在不适用任何JS代码情况下即可实现响应式布局的轻量级框架,无依赖,体积小. 1. CDN ...

随机推荐

  1. IE6下整站的bug详解

    <1>文字不居中:加一个行高: <2>png文件作为背景不显示: <!--[if IE 6]> <script src="js/DD_belated ...

  2. Lucene入门教程

    Lucene教程 1 lucene简介 1.1 什么是lucene     Lucene是一个全文搜索框架,而不是应用产品.因此它并不像www.baidu.com 或者google Desktop那么 ...

  3. .Net多线程编程—Parallel LINQ、线程池

    Parallel LINQ 1 System.Linq.ParallelEnumerable 重要方法概览: 1)public static ParallelQuery<TSource> ...

  4. 在 Linux 系统中安装Load Generator ,并在windows 调用方法

    在 Linux 系统中安装Load Generator ,并在windows 调用 由于公司需要测试系统的最大用户承受能力,所以需要学习使用loadrunner.在安装的时候碰到了不少问题,所以写下此 ...

  5. 使用nginx简单实现负载均衡

    只是简单使用nginx玩玩而已,知道能这么用,但是在实际项目中并没有实践过,在项目不大的时候用不到,但是对于理解负载均衡来说还是可以的. 利用虚拟机安装了三个centOS系统,然后顺便装了环境. 这里 ...

  6. leveldb性能分析

    Leveldb是一个google实现的非常高效的kv数据库,目前的版本1.2能够支持billion级别的数据量了. 在这个数量级别下还有着非常高的性能,主要归功于它的良好的设计.特别是LSM算法. 那 ...

  7. 功能强大的HTML

    HTML基本标签(一) 1.什么是HTML html:Hyper TextMakeup language:超文本标记语言 html:网页的“源码” 浏览器:“解释和执行”html源码的工具 2.网页的 ...

  8. [转]iptables

    iptables ptables简介 iptables是基于内核的防火墙,功能非常强大,iptables内置了filter,nat和mangle三张表. filter负责过滤数据包,包括的规则链有,i ...

  9. Posix消息队列实现机制

    本文是对<Unix 网络编程 卷2:进程通信>的笔记. 引言 消息队列是进程间通信的一种方式,可是如果不理解他的实现原理,会有众多不理解之处,下面就结合本书中的例子,对posix消息队列来 ...

  10. linux下提示bash:command not found

    新安装的linux系统,如果进行精简安装可能会出现bash:command not found 的提示,大家在安装的时候可以选择默认安装basic的组件,一般即可.到时候可以再升级.   如果新装的系 ...