介绍地址:http://www.cocoachina.com/ios/20141219/10702.html

官网:https://github.com/SnapKit/Masonry

记住:一定要先添加View , 再设置位置

NSLayout 相关枚举

typedef NS_ENUM(NSInteger, NSLayoutRelation) {
NSLayoutRelationLessThanOrEqual = -1, //小于等于
NSLayoutRelationEqual = 0, //等于
NSLayoutRelationGreaterThanOrEqual = 1, //大于等于
};
typedef NS_ENUM(NSInteger, NSLayoutAttribute) {
NSLayoutAttributeLeft = 1, //左侧
NSLayoutAttributeRight, //右侧
NSLayoutAttributeTop, //上方
NSLayoutAttributeBottom, //下方
NSLayoutAttributeLeading, //首部
NSLayoutAttributeTrailing, //尾部
NSLayoutAttributeWidth, //宽度
NSLayoutAttributeHeight, //高度
NSLayoutAttributeCenterX, //X轴中心
NSLayoutAttributeCenterY, //Y轴中心
NSLayoutAttributeBaseline, //文本底标线 NSLayoutAttributeNotAnAttribute = 0 //没有属性
};

左右顶部宽高一样:

make.left.right.and.top.equalTo(self);

实际使用

/**
* 底部购买栏 初始化
*/
- (void)setupFooterView
{
CGFloat footerViewH = ; // 背景栏
UIView *footerView = [[UIView alloc] init];
footerView.backgroundColor = [UIColor whiteColor];
footerView.layer.borderColor = [UIColor colorWithHexString:Color_Line].CGColor;
footerView.layer.borderWidth = 0.3;
[self.view addSubview:footerView]; // 首页
UIButton *homeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
homeBtn.titleLabel.font = [UIFont systemFontOfSize:];
homeBtn.layer.borderWidth = 0.3;
homeBtn.layer.borderColor = [UIColor colorWithHexString:Color_Line].CGColor;
[homeBtn setTitle:@"首页" forState:UIControlStateNormal];
[homeBtn setTitleColor:[UIColor colorWithHexString:@"6c6c6c"] forState:UIControlStateNormal];
[homeBtn setImage:[UIImage imageNamed:@"home"] forState:UIControlStateNormal];
[homeBtn setImageEdgeInsets:UIEdgeInsetsMake(-, 12.5, , )];
[homeBtn setTitleEdgeInsets:UIEdgeInsetsMake(, -, , )];
[homeBtn addTarget:self action:@selector(homeClick) forControlEvents:UIControlEventTouchUpInside];
[footerView addSubview:homeBtn]; // 加入购物车
UIButton *addCartBtn = [UIButton buttonWithType:UIButtonTypeCustom];
addCartBtn.backgroundColor = [UIColor colorWithHexString:@"fc714b"];
addCartBtn.titleLabel.font = [UIFont systemFontOfSize:];
[addCartBtn setTitle:@"加入购物车" forState:UIControlStateNormal];
[addCartBtn setTitleColor:[UIColor colorWithHexString:@"ffffff"] forState:UIControlStateNormal];
[addCartBtn setBackgroundImage:[Tool createImageWithColor:[UIColor colorWithHexString:Color_Line]] forState:UIControlStateSelected];
[addCartBtn addTarget:self action:@selector(buyClick:) forControlEvents:UIControlEventTouchUpInside];
[footerView addSubview:addCartBtn];
if (![self.goodsDetail.goods.only_one_step_buy isEqualToString:@""]) {
addCartBtn.selected = YES;
addCartBtn.userInteractionEnabled = NO;
} // 立即购买
UIButton *buyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
buyBtn.backgroundColor = [UIColor colorWithHexString:Color_Red];
buyBtn.titleLabel.font = [UIFont systemFontOfSize:];
[buyBtn setTitle:@"立即购买" forState:UIControlStateNormal];
[buyBtn setTitleColor:[UIColor colorWithHexString:@"ffffff"] forState:UIControlStateNormal];
[buyBtn setBackgroundImage:[Tool createImageWithColor:[UIColor colorWithHexString:@"e8e8e8"]] forState:UIControlStateSelected];
[buyBtn addTarget:self action:@selector(buyClick:) forControlEvents:UIControlEventTouchUpInside];
[footerView addSubview:buyBtn]; // 库存为0,底部按钮变灰
if ([self.goodsDetail.goods.goods_number intValue] <= ) {
buyBtn.selected = YES;
buyBtn.userInteractionEnabled = NO; addCartBtn.selected = YES;
addCartBtn.userInteractionEnabled = NO;
} [footerView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(ScreenWidth,footerViewH));
make.bottom.mas_equalTo(@);
}]; [homeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(footerView.mas_top);
make.left.equalTo(footerView.mas_left);
make.bottom.equalTo(footerView.mas_bottom);
make.width.equalTo(@);
make.height.equalTo(footerView.mas_height);
}]; [addCartBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(footerView.mas_top);
make.left.equalTo(homeBtn.mas_right);
make.bottom.equalTo(footerView.mas_bottom);
make.right.equalTo(buyBtn.mas_left); make.width.equalTo(@[addCartBtn,buyBtn]);
make.height.equalTo(footerView.mas_height);
}]; [buyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(footerView.mas_top);
make.left.equalTo(addCartBtn.mas_right);
make.bottom.equalTo(footerView.mas_bottom);
make.right.equalTo(footerView.mas_right); make.width.equalTo(@[addCartBtn,buyBtn]);
make.height.equalTo(footerView.mas_height);
}]; _footerView = footerView;
}

效果:

两个元素相对居中

效果红色框如:

具体代码:

    UIView *couponView = [[UIView alloc] init];
couponView.backgroundColor = [UIColor redColor];
[payInfoView addSubview:couponView]; //父控件 加入 容器
[couponView mas_makeConstraints:^(MASConstraintMaker *make) {
//make.height.top.centerX.equalTo(couponView.superview);//width不设置,让其根据子控件宽度进行适配
make.centerX.equalTo(payInfoView);
make.height.mas_equalTo(payInfoViewFooterH);
make.bottom.mas_equalTo(payInfoView.mas_bottom);
}]; // 已优惠
UILabel *couponLabel = [[UILabel alloc] init];
couponLabel.textColor = VSColorFromRGB(0x585c64);
couponLabel.font = [UIFont systemFontOfSize:];
[couponView addSubview:couponLabel];
couponLabel.text = [NSString stringWithFormat:@"已优惠¥%@",@""]; // 点击详情
UIButton *couponLookDescButton = [UIButton buttonWithType:UIButtonTypeCustom];
[couponLookDescButton setImage:[UIImage imageNamed:@"pay_instalment_explain"] forState:UIControlStateNormal];
[couponView addSubview:couponLookDescButton]; [couponLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(couponView);
make.centerY.equalTo(couponView);
make.right.equalTo(couponLookDescButton.mas_left).offset(-);
}]; [couponLookDescButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(couponLabel.mas_right).offset();
make.centerY.equalTo(couponView);
make.right.equalTo(couponView);
}];

计算自动约束高度

    // 最终总高度
[_payPeriodBgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self);
make.left.right.equalTo(self);
make.bottom.equalTo(_periodDetailListView.mas_bottom);
}]; // 更新约束
[self layoutIfNeeded]; // 视图的最大 Y 值
CGFloat h= CGRectGetMaxY(_payPeriodBgImageView.frame);

mas_makeConstraints &&  mas_remakeConstraints && mas_updateConstraints 用法与注意事项

1 mas_makeConstraints 只负责新增约束,Autolayout 不能同时存在两条针对于同一对象约束 否则会报错 
注意:在添加约束之前一定要添加到view( [testView addSubview:self.view])上 否则会报错

[testView mas_makeConstraints:^(MASConstraintMaker *make) { 
make.leading.equalTo(view1.mas_trailing).offset(10); 
make.bottom.equalTo(view2.mas_bottom).offset(-2); 
make.height.mas_equalTo(70); 
make.width.mas_equalTo(50); 
}];

2 mas_remakeConstraints 会清除之前的所有约束 仅保留最新的约束

如下:会清除原来的以view1 和view2 为参照的约束 保留 以view3 和view4 为参照的约束 
[testView mas_remakeConstraints :^(MASConstraintMaker *make) { 
make.bottom.equalTo(view3.mas_top).offset(-10); 
make.centerX.equalTo(view4.mas_centerX); 
make.height.mas_equalTo(70); 
make.width.mas_equalTo(50); 
}];

3 mas_updateConstraints 针对上面的情况 会更新block中出现的约束 不会导致出现两个相同的约束的情况

注意: 同一个对象 使用mas_updateConstraints 一定要保证 block中要更新的元素是 其使用mas_makeConstraints 设置的约束

如下就是正确的 则不会出现约束冲突问题 
[testView mas_updateConstraints:^(MASConstraintMaker *make) { 
make.bottom.equalTo(view1.mas_top).offset(-60); 
必须是 同一参照对象(都是以view1)同一参照属性(都是以view1的mas_top) 
}];

但是下面的就不对了 就会出现约束冲突 在iOS 7 及其以下会出现由于约束冲突出现的崩溃

[testView mas_updateConstraints:^(MASConstraintMaker *make) { 
make.bottom.equalTo(view2.mas_top).offset(-10); 
make.centerX.equalTo(view4.mas_centerX); 
原因1:不同一参照对象 (原来是view1 现在是view2) 
原因2 testView 在 mas_makeConstraints中没有设置 center 所以无法更新 
}];

解决办法:使用mas_remakeConstraints 清除原来的约束 重新设置约束; 
}];

其它可参考:http://www.jianshu.com/p/2b57ece2b3b8

iOS 第三方框架-Masonry的更多相关文章

  1. 使用第三方框架 Masonry 实现自动布局

    使用第三方框架 Masonry 实现自动布局 时间:2015-02-10 11:08:41      阅读:4595      评论:0      收藏:0      [点我收藏+] 标签: 由于前两 ...

  2. ios开发学习笔记040-autolayout 第三方框架Masonry

    不管是是界面创建约束还是代码创建约束,苹果官方提供的方式都比较繁琐.所以出现了第三方框架. Masonry 在github地址如下: https://github.com/SnapKit/Masonr ...

  3. iOS 自动布局框架 – Masonry 详解

    目前iOS开发中大多数页面都已经开始使用Interface Builder的方式进行UI开发了,但是在一些变化比较复杂的页面,还是需要通过代码来进行UI开发的.而且有很多比较老的项目,本身就还在采用纯 ...

  4. iOS自动布局框架-Masonry详解

    首先,在正式使用Masonry之前,我们先来看看在xib中我们是如何使用AutoLayout     从图中我们可以看出,只要设置相应得局限,控制好父视图与子视图之间的关系就应该很ok的拖出你需要的需 ...

  5. iOS 第三方框架-SDWebImage

    iOS中著名的牛逼的网络图片处理框架.包含的功能:图片下载.图片缓存.下载进度监听.gif处理等等.用法极其简单,功能十分强大,大大提高了网络图片的处理效率.国内超过90%的iOS项目都有它的影子. ...

  6. iOS:第三方框架MJPhotoBrowser图片浏览器的使用

    介绍:MJPhotoBrowser这个第三方库是MJ老师封装的一套用来浏览图片的浏览器,可是是本地图片.网络图片.gif图片等,其也依赖了SDWebImage.SVProgressHUD.YLGIFI ...

  7. iOS 第三方框架-MJRefresh

    MJRefresh是一款非常好用的上拉下拉第三方库,使用也很简单.github地址: https://github.com/CoderMJLee/MJRefresh . 下拉刷新 官方给过来的例子很简 ...

  8. iOS - 第三方框架 - AFN

    #5.AFNetworking 2.6使用方法 >2.6版本 支持 iOS7以上,而且支持NSURLConnectionOperation >3.0版本 支持 iOS7以上 NSURLCo ...

  9. iOS 第三方框架-MJExtension

    1.数组转换成模型数组 // 将 "微博字典"数组 转为 "微博模型"数组 NSArray *newStatuses = [HWStatus objectArr ...

随机推荐

  1. 从 Firefox 35 版本开始,就无法兼容 PAC 式代理

    经过反复的测试,包括在“高级”选项里启用 PAC 代理的设置,也都无法使用 PAC 的代理——无法登陆 Twitter 账号,无法打开 Google 网页. 不知道各位有什么好办法吗? 以及中文火狐社 ...

  2. ReactNative小笔记

    import React, { Component } from 'react'; import { AppRegistry, View } from 'react-native'; export d ...

  3. RabbitMQ 分布式设置和高可用性讨论

    abbitMQ的集群主要有配置方式,分别是:本地局域网Cluster,federation,shovel. RabbitMQ Cluster主要是用于同一个网段内的局域网. federation和sh ...

  4. 解决双系统(Window10+Ubuntu16.10)下ubuntu安装git时提示软件包git没有可安装候选问题

    选择升级系统: sudo apt-get update 升级之后再输入: sudo apt-get install git 可成功安装.

  5. PostgreSQL 自动输入密码

    在 Shell 命令行中,使用 postgresql-client 连接 PostgreSQL 数据库时,每次都要输入密码.如果要写 Shell Script,做一些类似于备份的自动化管理工作,每次都 ...

  6. 内核poll机制

    内核版本:linux2.6.22.6 硬件平台:JZ2440 驱动源码 poll_key_int_drv.c : #include <linux/module.h> #include &l ...

  7. 转载:caffe中的Reshape层

    http://blog.csdn.net/terrenceyuu/article/details/76228317 #作用:在不改变数据的情况下,改变输入的维度 layer { name: " ...

  8. 对vue.js的template编译的理解

    简而言之,就是先转化成AST树,再得到的render函数返回VNode(Vue的虚拟DOM节点) 详情步骤: 首先,通过compile编译器把template编译成AST语法树(abstract sy ...

  9. Android 出现 activity supporting action_view is not set as browsable报错

    <activity android:name=".ThirdActivity"> <intent-filter> <action android:na ...

  10. java public,default,protected,private区别

    温习一下:https://www.cnblogs.com/ldq2016/p/6872420.html