Under the hood Auto Layout is a powerful and flexible way of organising and laying out your views. However creating constraints from code is verbose and not very descriptive. Imagine a simple example in which you want to have a view fill its superview but inset by 10 pixels on every side

UIView *superview = self.view;

UIView *view1 = [[UIView alloc] init];
view1.translatesAutoresizingMaskIntoConstraints = NO;
view1.backgroundColor = [UIColor greenColor];
[superview addSubview:view1]; UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10); [superview addConstraints:@[ //view1 constraints
[NSLayoutConstraint constraintWithItem:view1
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:superview
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:padding.top], [NSLayoutConstraint constraintWithItem:view1
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:superview
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:padding.left], [NSLayoutConstraint constraintWithItem:view1
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:superview
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:-padding.bottom], [NSLayoutConstraint constraintWithItem:view1
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:superview
attribute:NSLayoutAttributeRight
multiplier:1
constant:-padding.right], ]];

Even with such a simple example the code needed is quite verbose and quickly becomes unreadable when you have more than 2 or 3 views. Another option is to use Visual Format Language (VFL), which is a bit less long winded. However the ASCII type syntax has its own pitfalls and its also a bit harder to animate as NSLayoutConstraint constraintsWithVisualFormat: returns an array.

Prepare to meet your Maker!

Heres the same constraints created using MASConstraintMaker

UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);

[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(superview.mas_top).with.offset(padding.top); //with is an optional semantic filler
make.left.equalTo(superview.mas_left).with.offset(padding.left);
make.bottom.equalTo(superview.mas_bottom).with.offset(-padding.bottom);
make.right.equalTo(superview.mas_right).with.offset(-padding.right);
}];

Masonry 原理一的更多相关文章

  1. Masonry 原理与使用说明

    原理: 1)约束生成:MASConstraintMaker: 2)缺省补齐: - (void)setSecondViewAttribute:(id)secondViewAttribute { if ( ...

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

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

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

    接上篇,本篇继续对Masonry 进行学习,接上篇示例: (6)Masonry 布局实现iOS 计算器 - (void)exp4 { WS(weakSelf); // 申明区域,displayView ...

  4. Masonry学习分享

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

  5. Masonry使用注意篇

    简要 自动布局最重要的是约束:UI元素间关系的数学表达式.约束包括尺寸.由优先级和阈值管理的相对位置.它们是添加剂,可能导致约束冲突 .约束不足造成布局无法确定 .这两种情况都会产生异常. 使用前:A ...

  6. AutoLayout框架Masonry使用心得

    AutoLayout框架Masonry使用心得 字数1769 阅读1481 评论1 喜欢17 我们组分享会上分享了页面布局的一些写法,中途提到了AutoLayout,会后我决定将很久前挖的一个坑给填起 ...

  7. iOS开发通过代码方式使用AutoLayout (NSLayoutConstraint + Masonry)

    iOS开发通过代码方式使用AutoLayout (NSLayoutConstraint + Masonry) 随着iPhone6/6+设备的上市,如何让手头上的APP适配多种机型多种屏幕尺寸变得尤为迫 ...

  8. iOS masonry九宫格 单行 多行布局

    Masonry是个好东西,在当前尺寸各异的iOS开发适配中发挥着至关重要的作用,由于项目中Masonry布局用的比较多,对于UI布局也有了一些自己的理解,经常会有人问道Masonry布局九宫格要怎么布 ...

  9. iOS学习——布局利器Masonry框架源码深度剖析

    iOS开发过程中很大一部分内容就是界面布局和跳转,iOS的布局方式也经历了 显式坐标定位方式 --> autoresizingMask --> iOS 6.0推出的自动布局(Auto La ...

随机推荐

  1. CodeForces484A Bits(贪心)

    CodeForces484A Bits(贪心) CodeForces484A 题目大意:给出范围[A.B].期望你给出某个数X满足X属于[A,B],而且X转成二进制的1的个数最多.假设有多个给出最小的 ...

  2. 【SSO】--单点登录之过滤器(filter)

    在单点登录的探索中.用到一个知识点:过滤器(filter).常见的几种验证:Authorization filters,验证用户是否有权限訪问页面:Action Filter,验证用户登录的时候是否用 ...

  3. COCOS学习笔记--即时动作ActionInstant

    Cocos引擎中的动作类的关系图例如以下: 能够看出,Action是继承自Ref类的,之前我的博客中也有讲过,Ref类是cocos2dx全部类的基类.动作类(Action)是全部动作的基类.它通过cr ...

  4. 【bzoj4538】[Hnoi2016]网络

    我们考虑树剖,线段树上维护一个堆,保存不经过该段区间的路径的权值. 对于一条路径我们将对于线段树中的区间提取出来,在对于线段树中进行修改.也就是在堆中插入或删除. 对于一次询问,只要找到包含该点的线段 ...

  5. 17.EXTJs 中icon 与iconCls的区别及用法!

    转自:https://blog.csdn.net/u013890437/article/details/38315717?utm_source=blogxgwz7

  6. 使用IntelliJ IDEA 配置JDK(入门)

    一.JDK下载 首先要下载java开发工具包JDK,下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html 点击 ...

  7. FastDFS的介绍

    FastDFS的介绍 FastDFS FastDFS是由国人余庆所开发,其项目地址: https://github.com/happyfish100 FastDFS是一个轻量级的开源分布式文件系统,主 ...

  8. MogileFS介绍

    MogileFS介绍 MogileFS 是一个开源的分布式文件存储系统,由LiveJournal旗下的Danga Interactive公司开发. Danga团队开发了包括 Memcached.Mog ...

  9. E20170612-sl

     tampon n.     卫生棉塞; 止血棉塞;    sanitary  n. 公共厕所;  adj.     卫生的; 清洁的;    belonging n.     附属品,附件,属性; ...

  10. Vigenère密码 2012年NOIP全国联赛提高组(字符串模拟)

    P1079 Vigenère 密码 题目描述 16 世纪法国外交家 Blaise de Vigenère 设计了一种多表密码加密算法――Vigenère 密 码.Vigenère 密码的加密解密算法简 ...