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 控件分类 : 活 ...
随机推荐
- iOS block并发
多核运算 在iOS中concurrency编程的框架就是GCD(Grand Central Dispatch), GCD的使用非常简单.它把任务分派到不同的queue队列来处理.开发者把任务代码装到一 ...
- 转载sql server 关于 default value的一些使用总结
转载原出处:http://blog.csdn.net/miqi770/article/details/6728733 1.在创建表的时候,给字段添加的默认值约束 CREATE TABLE " ...
- TFS的使用
1.http://www.kwstu.com/ArticleView/kwstu_201462311500744
- 解决ecshop在线客户点击无法唤醒QQ问题
找到default/library/page_footer.lbi中找到QQ代码的相应位置,然后你会发现之前模板里面为什么QQ点击不能对话,是因为QQ客服安装包中的JS代码有的可能是比较旧的代码了. ...
- ALV可输入状态下输入金额字段变小数的问题
http://blog.163.com/mxb_sap@yeah/blog/static/10335262520167109022155/ 小数位数两位 当我在给ALV上给该字段输入整数 '12 ...
- 对get_baserel_parampathinfo函数的学习
/* * get_baserel_parampathinfo * Get the ParamPathInfo for a parameterized path for a base relation, ...
- thinPHP中多维数组的遍历
$drug=array( 'ACEI'=>array(array('ch_name'=>'卡托普利','en_name'=>'captopril'),array('ch_nam ...
- 在iOS中怎样创建可展开的Table View?(上)
原文地址 本文作者:gabriel theodoropoulos 原文:How To Create an Expandable Table View in iOS 原文链接 几乎所有的app都有一个共 ...
- ios开发——实用技术OC-Swift篇&本地通知与远程通知详解
本地通知与远程通知详解 一:本地通知 Local Notification的作用 Local Notification(本地通知) :是根据本机状态做出的通知行为,因此,凡是仅需依赖本机状态即可判 ...
- Firefly distributed模块的原理与twisted中PB远程调用协议
这些天断断续续在看Firefly, 看了一下distributed模块的设计,其实就是使用的twisted.spread.pb觉得以后要是想用Firefly有必要了解一下twisted, 所以在网上查 ...