Masonry 原理一
2024-10-12 10:08:55
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);
}];
最新文章
- Empty 和空字符串区别
- 观 GT Java语言管理系统的感悟
- VBS 获取当前进程 ID
- JavaScript学习(一)—处理事件
- Insert select 带选择复制一张表到另一张表
- Metro之Popup控件的使用(登录)
- 单源最短路径-Dijkstra算法
- Mcafee两个Mac版本之间的区别
- Java笔记(二十六)……IO流上 字节流与字符流
- Spring注解与Spring与Struts2整合
- 学习python登录demo
- python3.5 默认安装路径 | 安装 | 删除
- vs2013下配置opencv1.0.0
- 使用css将图像居中
- 超实用!9个目前流行的MATERIAL DESIGN前端框架
- AngularJS简单入门
- 使用Google-Colab训练PyTorch神经网络
- 2.TypeScript 基础入门(二)
- sql表设计
- 二:Java之异常处理
热门文章
- 七夕节 看到很多停止更新的blog 莫名有点淡淡的忧桑
- frameset怎样实现整个页面的跳转
- 通讯编程入门--WEBSOCKET
- HDU 5324 Boring Class【cdq分治】
- ZOJ3662:Math Magic(全然背包)
- luogu1314 聪明的质检员
- HTTP要点概述:十一,HTTP状态码
- c# Java 微信红包算法
- bzoj3550: [ONTAK2010]Vacation&;&;bzoj3112: [Zjoi2013]防守战线
- Java:EL表达式