Masonry 控件详解
1. 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; //文本基线
2.Masonry给我们提供了3个方法
//新增约束
-
(NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *make))block;
//更新约束
- (NSArray *)mas_updateConstraints:(void(^)(MASConstraintMaker
*make))block;
//清楚之前的所有约束,只会保留最新的约束
- (NSArray *)mas_remakeConstraints:(void(^)(MASConstraintMaker
*make))block;
3.常见约束的各种类型
1.尺寸:width、height、size
2.边界:left、leading、right、trailing、top、bottom
3.中心点:center、centerX、centerY
4.边界:edges
5.偏移量:offset、insets、sizeOffset、centerOffset
6.priority()约束优先级(0~1000),multipler乘因数,
dividedBy除因数
4.Masonry约束易忽略的技术点
使用Masonry不需要设置控件的translatesAutoresizingMaskIntoConstraints属性为NO;
防止block中的循环引用,使用弱引用(这是错误观点),在这里block是局部的引用,block内部引用self不会造成循环引用的
__weak typeof
(self) weakSelf = self;(没必要的写法)
5.Masonry约束控件出现冲突的问题
当约束冲突发生的时候,我们可以设置view的key来定位是哪个view
redView.mas_key =
@"redView";
greenView.mas_key
= @"greenView";
blueView.mas_key =
@"blueView";
若是觉得这样一个个设置比较繁琐,怎么办呢,Masonry则提供了批量设置的宏MASAttachKeys
MASAttachKeys(redView,greenView,blueView);
//一句代码即可全部设置
6. equalTo 和 mas_equalTo的区别
mas_equalTo只是对其参数进行了一个BOX(装箱) 操作,目前支持的类型:数值类型(NSNumber)、 点(CGPoint)、大小(CGSize)、边距(UIEdgeInsets),而equalTo:这个方法不会对参数进行包装。
7.Masonry 布局
make.top.equalTo(view).with.offset(10);//
距离上10
make.left.equalTo(view).with.offset(10);//距离左10
make.bottom.equalTo(view).with.offset(-10);//距离下10
make.right.equalTo(view).with.offset(-10);//距离右10
等同于make.edges.mas_offset(UIEdgeInsetsMake(10,10,10,10));
等高 \等宽
make.height.mas_equalTo(@[redView, blueView]);
make.width.mas_equalTo(@[redView, blueView]);
最大值
make.width.height.lessThanOrEqualTo(@250);
最大放大到整个view
make.width.height.lessThanOrEqualTo(self.view);
最小值make.width.height.greaterThanOrEqualTo(@90);
优先级最低
make.width.height.mas_equalTo(100
* self.scacle).priorityLow();
设置高/宽为3:1,要求是同一个控件的属性比例
make.height.mas_equalTo(bottomInnerView.mas_width).multipliedBy(3);
*
axisType 轴线方向
*
fixedSpacing 间隔大小
*
fixedItemLength 每个控件的固定长度/宽度
*
leadSpacing 头部间隔
*
tailSpacing 尾部间隔
//首先添加5个视图
NSMutableArray *array = [NSMutableArray new];
for (int i = 0; i < 5; i ++) {
UIView *view = [UIView new];
view.backgroundColor = [UIColor
greenColor];
[self addSubview:view];
[array addObject:view]; //保存添加的控件
}
水平方向控件间隔固定等间隔
[array
mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:15
leadSpacing:10 tailSpacing:10];
[array
makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(50);
make.height.equalTo(70);
}];
水平方向宽度固定等间隔
[array mas_distributeViewsAlongAxis:MASAxisTypeHorizontal
withFixedItemLength:70 leadSpacing:10 tailSpacing:10];
[array
makeConstraints:^(MASConstraintMaker *make) { //数组额你不必须都是view
make.top.equalTo(50);
make.height.equalTo(70);
}];
设置preferredMaxLayoutWidth: 多行label约束的完美解决
[self.label
makeConstraints:^(MASConstraintMaker *make) {
make.left.top.equalTo(10);
make.right.equalTo(-10);
}];
更新约束
mas_updateConstraints
// 告诉self.view约束需要更新
[self.view setNeedsUpdateConstraints];
// 调用此方法告诉self.view检测是否需要更新约束,若需要则更新,下面添加动画效果才起作用
[self.view updateConstraintsIfNeeded];
[UIView animateWithDuration:0.3 animations:^{
[self.view layoutIfNeeded];
}];
- (void)updateViewConstraints {
[self.growingButton mas_updateConstraints:^(MASConstraintMaker *make) {
}];
[super updateViewConstraints];
}
Masonry 控件详解的更多相关文章
- IOS—UITextFiled控件详解
IOS—UITextFiled控件详解 //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGR ...
- picker控件详解与使用,(实现省市的二级联动)
picker控件详解与使用,(实现省市的二级联动) 第一步:新建一个单视图(single view)的工程, 命名为pickerTest,不要勾选下面两个选项,第一个是新版本里面的,第二个是单元测试, ...
- Switch控件详解
Switch控件详解 原生效果 5.x 4.x 布局 <Switch android:id="@+id/setting_switch" android:layout_widt ...
- ToolBar控件详解
ToolBar控件详解 在Activity中添加ToolBar 1.添加库 dependencies { ... compile "com.android.support:appcompat ...
- Spinner控件详解
Spinner控件详解 效果图 修改Spinner样式 在介绍之前,先看一下系统原生的样式 6.x & 5.x系统样式 4.x系统样式 官方文档 XML属性 方法 描述 android:dro ...
- Android开发:文本控件详解——TextView(一)基本属性
一.简单实例: 新建的Android项目初始自带的Hello World!其实就是一个TextView. 在activity_main.xml中可以新建TextView,从左侧组件里拖拽到右侧预览界面 ...
- Android开发:文本控件详解——TextView(二)文字跑马灯效果实现
一.需要使用的属性: 1.android:ellipsize 作用:若文字过长,控制该控件如何显示. 对于同样的文字“Android开发:文本控件详解——TextView(二)文字跑马灯效果实现”,不 ...
- C++ CComboBox控件详解
转载:http://blog.sina.com.cn/s/blog_46d93f190100m395.html C++ CComboBox控件详解 (2010-09-14 14:03:44) 转载▼ ...
- 【iOS 开发】基本 UI 控件详解 (UIButton | UITextField | UITextView | UISwitch)
博客地址 : http://blog.csdn.net/shulianghan/article/details/50051499 ; 一. UI 控件简介 1. UI 控件分类 UI 控件分类 : 活 ...
随机推荐
- mac linux netstat -n
一直都不明白为什么mac就是不现实8080端口,现在明白了 USERtekiiMac-3:~ user$ netstat -an | grep tcp46 tcp46 0 0 *.8009 *.* L ...
- Auto Layout 使用心得
此系列文章代码仓库在 https://github.com/johnlui/AutoLayout ,有不明白的地方可以参考我的 Auto Layout 设置哦,下载到本地打开就可以了. 简介 Auto ...
- 【Matlab】随机游走产生图像效果
随机游走类似布朗运动,就是随机的向各个方向走吧.产生的图像实在漂亮,所以还是贴出分享. clear all; close all; clc; n=100000; x= 0; y= 0; pixel=z ...
- ecshop中index.dwt文件分析
对于ecshop新手来说,这篇总结值得关注. 对于没有web编程基础的同学来说,ecshop模板里面有两个文件特别重要, 但是这两个文件同时也很不好理解,分别是index.dwt和style.css. ...
- JavaScript要点(十) HTML DOM - 改变 HTML
HTML DOM 允许 JavaScript 改变 HTML 元素的内容. A.改变 HTML 输出流 JavaScript 能够创建动态的 HTML 内容: 今天的日期是: Thu Oct 13 2 ...
- POJ 1703 Find them, catch them (并查集)
题目:Find them,Catch them 刚开始以为是最基本的并查集,无限超时. 这个特殊之处,就是可能有多个集合. 比如输入D 1 2 D 3 4 D 5 6...这就至少有3个集合了.并且 ...
- TortoiseGit disconnected: no supported authentication methods available(server sent:publickey)
之前一直用命令行,现在想用图形工具,TortoiseGit,安装后遇到错误 TortoiseGit disconnected: no supported authentication methods ...
- SPOJ - OTOCI LCT
OTOCI Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/problem/viewProblem. ...
- 网络子系统55_ip协议分片重组_加入ipq
//ip分片加入到正确的ipq结构 //调用路径:ip_defrag->ip_frag_queue // 处理过程: // 1.正在被释放的ipq,不处理新加入的分片(ipq正在被释放由last ...
- 征服 Nginx + Tomcat
2年前一直折腾Apache,现如今更习惯Nginx. 搭建网站又遇到2年前遇到的问题——Session同步. (参考我以前的帖子——征服 Apache + Tomcat)只不过现今担当负载均衡的Apa ...