介绍地址: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. nginx js、css、图片 及 一些静态文件中出现 http://upstreamname:port 导致部分网页样式显示不正常

    nginx js.css.图片 及 一些静态文件中出现 http://upstreamname:port 导致部分网页样式显示不正常 http://upstreamname:port/....../. ...

  2. 终于知道什么情况下需要实现.NET Core中的IOptions接口

    自从接触 IOptions 之后,一直纠结这样的问题:自己定义的 Options 要不要实现 IOptions 接口. 微软有的项目中实现了,比如 Caching 中的 MemoryCacheOpti ...

  3. [No0000E7]C# 封装 与访问修饰符

    C# 支持的访问修饰符: Public Private Protected Internal Protected internal Public 访问修饰符 Public 访问修饰符允许一个类将其成员 ...

  4. ms sql server,oracle数据库实现拼接一列的多行内容

    项目中要将查询出的一列的多行内容拼接成一行,如下图:ypmc列. ms sql server: 网上查到相关资料如下:http://blog.csdn.net/rolamao/article/deta ...

  5. HBase 笔记

    参考资料:HBase权威指南 一行由若干列组成,若干列又构成一个列族一个列族的所有列存储在同一个底层的存储文件里,这个文件叫HFile列族的数量有限制:一个列族里列的数量没限制谓词删除:例如允许用户只 ...

  6. 转:mysql where group by having

    原文地址:https://blog.csdn.net/tengdazhang770960436/article/details/6992272 1.where为什么要写在group by之前呢? 因为 ...

  7. Appium入门(9)—— Appium API

    摘自:http://www.testclass.net/appium/appium-base-api-01/ 1.安装: installApp() driver.installApp("d: ...

  8. kafka可视化工具kafka tools

    一.下载 下载地址 选择windows 傻瓜式安装,选择安装路径,直接下一步就可以了 二. 使用 点击,运行 linux开启9092(broker)端口和2181(zookeeper)然后填写后,确定 ...

  9. 【Python全栈-JavaScript】JavaScript入门

    JavaScript基础知识点 一.JavaScript概述 参考:http://www.w3school.com.cn/b.asp JavaScript的历史 1.1992年Nombas开发出C-m ...

  10. PHP 快速实现大文件上传

    简单的上传代码 最简上传代码 <?php move_uploaded_file($_FILES["file"]["tmp_name"], $_FILES[ ...