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);
}];

最新文章

  1. Empty 和空字符串区别
  2. 观 GT Java语言管理系统的感悟
  3. VBS 获取当前进程 ID
  4. JavaScript学习(一)—处理事件
  5. Insert select 带选择复制一张表到另一张表
  6. Metro之Popup控件的使用(登录)
  7. 单源最短路径-Dijkstra算法
  8. Mcafee两个Mac版本之间的区别
  9. Java笔记(二十六)……IO流上 字节流与字符流
  10. Spring注解与Spring与Struts2整合
  11. 学习python登录demo
  12. python3.5 默认安装路径 | 安装 | 删除
  13. vs2013下配置opencv1.0.0
  14. 使用css将图像居中
  15. 超实用!9个目前流行的MATERIAL DESIGN前端框架
  16. AngularJS简单入门
  17. 使用Google-Colab训练PyTorch神经网络
  18. 2.TypeScript 基础入门(二)
  19. sql表设计
  20. 二:Java之异常处理

热门文章

  1. 七夕节 看到很多停止更新的blog 莫名有点淡淡的忧桑
  2. frameset怎样实现整个页面的跳转
  3. 通讯编程入门--WEBSOCKET
  4. HDU 5324 Boring Class【cdq分治】
  5. ZOJ3662:Math Magic(全然背包)
  6. luogu1314 聪明的质检员
  7. HTTP要点概述:十一,HTTP状态码
  8. c# Java 微信红包算法
  9. bzoj3550: [ONTAK2010]Vacation&&bzoj3112: [Zjoi2013]防守战线
  10. Java:EL表达式