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 控件详解的更多相关文章

  1. IOS—UITextFiled控件详解

    IOS—UITextFiled控件详解 //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGR ...

  2. picker控件详解与使用,(实现省市的二级联动)

    picker控件详解与使用,(实现省市的二级联动) 第一步:新建一个单视图(single view)的工程, 命名为pickerTest,不要勾选下面两个选项,第一个是新版本里面的,第二个是单元测试, ...

  3. Switch控件详解

    Switch控件详解 原生效果 5.x 4.x 布局 <Switch android:id="@+id/setting_switch" android:layout_widt ...

  4. ToolBar控件详解

    ToolBar控件详解 在Activity中添加ToolBar 1.添加库 dependencies { ... compile "com.android.support:appcompat ...

  5. Spinner控件详解

    Spinner控件详解 效果图 修改Spinner样式 在介绍之前,先看一下系统原生的样式 6.x & 5.x系统样式 4.x系统样式 官方文档 XML属性 方法 描述 android:dro ...

  6. Android开发:文本控件详解——TextView(一)基本属性

    一.简单实例: 新建的Android项目初始自带的Hello World!其实就是一个TextView. 在activity_main.xml中可以新建TextView,从左侧组件里拖拽到右侧预览界面 ...

  7. Android开发:文本控件详解——TextView(二)文字跑马灯效果实现

    一.需要使用的属性: 1.android:ellipsize 作用:若文字过长,控制该控件如何显示. 对于同样的文字“Android开发:文本控件详解——TextView(二)文字跑马灯效果实现”,不 ...

  8. C++ CComboBox控件详解

    转载:http://blog.sina.com.cn/s/blog_46d93f190100m395.html C++ CComboBox控件详解 (2010-09-14 14:03:44) 转载▼ ...

  9. 【iOS 开发】基本 UI 控件详解 (UIButton | UITextField | UITextView | UISwitch)

    博客地址 : http://blog.csdn.net/shulianghan/article/details/50051499 ; 一. UI 控件简介 1. UI 控件分类 UI 控件分类 : 活 ...

随机推荐

  1. 松下蓄电池与UPS使用和维护

      使用条件及环境1.充电电流(浮充使用):0.15CA以下2.放电电流范围:0.05CA-3CA3.环境温度:0℃-40℃ (适宜的温度是25℃) 4.充电电压:(12V电池推荐值)   周围温度 ...

  2. 配dump文件

    ulimit -c unlimited echo 'ulimit -c unlimited' >>/etc/profile

  3. PowerDesigner 业务处理模型( BPM )

    PowerDesigner 业务处理模型( BPM ) 说明 properties语言文档xmlvalidation数据库   目录(?)[+]   一.     BPM 简介 业务处理模型(Busi ...

  4. 【转载】如何将Emmet安装到到 Sublime text 3?

    http://www.cnblogs.com/tinyphp/p/3217457.html 看清楚哦~~这是Sublime text 3不是2的版本,两者的安装还是有区别的,下面的方法是我感觉比较简单 ...

  5. IPAD之分割视图 SplitViewController

    转载自:http://www.w3cschool.cc/ios/att-ios-ui-splitview-htm.html 1 分割视图的使用 分割视图是 iPad 的特定视图控制器用于管理两个视图控 ...

  6. POJ 1703 Find them, catch them (并查集)

    题目:Find them,Catch them 刚开始以为是最基本的并查集,无限超时. 这个特殊之处,就是可能有多个集合. 比如输入D 1 2  D 3 4 D 5 6...这就至少有3个集合了.并且 ...

  7. HDU 5500 Reorder the Books 贪心

    Reorder the Books Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php? ...

  8. [置顶] 【玩转cocos2d-x之七】场景类CCScene和布景类CCLayer

    原创作品,转载请标明:http://blog.csdn.net/jackystudio/article/details/12708811 场景类CCScene和布景类CCLayer都是作为一个容器来使 ...

  9. [React Fundamentals] Owner Ownee Relationship

    The owner-ownee relationship is used to designate a parent-child relationship with React components ...

  10. Mysql一些重要配置参数的学习与整理系列

    http://my.oschina.net/realfighter/blog?catalog=585558&temp=1467909771588