iOS 第三方框架-Masonry
介绍地址: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的更多相关文章
- 使用第三方框架 Masonry 实现自动布局
使用第三方框架 Masonry 实现自动布局 时间:2015-02-10 11:08:41 阅读:4595 评论:0 收藏:0 [点我收藏+] 标签: 由于前两 ...
- ios开发学习笔记040-autolayout 第三方框架Masonry
不管是是界面创建约束还是代码创建约束,苹果官方提供的方式都比较繁琐.所以出现了第三方框架. Masonry 在github地址如下: https://github.com/SnapKit/Masonr ...
- iOS 自动布局框架 – Masonry 详解
目前iOS开发中大多数页面都已经开始使用Interface Builder的方式进行UI开发了,但是在一些变化比较复杂的页面,还是需要通过代码来进行UI开发的.而且有很多比较老的项目,本身就还在采用纯 ...
- iOS自动布局框架-Masonry详解
首先,在正式使用Masonry之前,我们先来看看在xib中我们是如何使用AutoLayout 从图中我们可以看出,只要设置相应得局限,控制好父视图与子视图之间的关系就应该很ok的拖出你需要的需 ...
- iOS 第三方框架-SDWebImage
iOS中著名的牛逼的网络图片处理框架.包含的功能:图片下载.图片缓存.下载进度监听.gif处理等等.用法极其简单,功能十分强大,大大提高了网络图片的处理效率.国内超过90%的iOS项目都有它的影子. ...
- iOS:第三方框架MJPhotoBrowser图片浏览器的使用
介绍:MJPhotoBrowser这个第三方库是MJ老师封装的一套用来浏览图片的浏览器,可是是本地图片.网络图片.gif图片等,其也依赖了SDWebImage.SVProgressHUD.YLGIFI ...
- iOS 第三方框架-MJRefresh
MJRefresh是一款非常好用的上拉下拉第三方库,使用也很简单.github地址: https://github.com/CoderMJLee/MJRefresh . 下拉刷新 官方给过来的例子很简 ...
- iOS - 第三方框架 - AFN
#5.AFNetworking 2.6使用方法 >2.6版本 支持 iOS7以上,而且支持NSURLConnectionOperation >3.0版本 支持 iOS7以上 NSURLCo ...
- iOS 第三方框架-MJExtension
1.数组转换成模型数组 // 将 "微博字典"数组 转为 "微博模型"数组 NSArray *newStatuses = [HWStatus objectArr ...
随机推荐
- 使用介质设备安装 AIX 以通过 HMC 安装分区
使用介质设备安装 AIX 以通过 HMC 安装分区 原文:https://www.ibm.com/support/knowledgecenter/zh/ssw_aix_72/com.ibm.aix.h ...
- You are using pip version 9.0.1, however version 18.0 is available. You should consider upgrading via the 'pip install --upgrade pip' command.
实测使用pip3进行一些软件安装的时候,会弹出这个,记住不要理会,一旦你执行,就会升级pip,并冲突pip3造成pip3不能用,这时候就要重新安装一下python3
- 深入理解无穷级数和的定义(the sum of the series)
Given an infinite sequence (a1, a2, a3, ...), a series is informally the form of adding all those te ...
- 确界原理 supremum and infimum principle 戴德金定理 Dedekind theorem
确界原理 supremum and infimum principle 戴德金定理 Dedekind theorem http://www.math.ubc.ca/~cass/courses/m ...
- eclipse debug模式
eclipse debug模式 1.怎样在Eclipse中设置断点 方法/步骤 1 首先打开工程项目 2 第一种是,把鼠标移动想要设置断点的行,在行号前面空白地方双击,就会出现断点 3 第二种是,在菜 ...
- 转:JAVAWEB开发之权限管理(二)——shiro入门详解以及使用方法、shiro认证与shiro授权
原文地址:JAVAWEB开发之权限管理(二)——shiro入门详解以及使用方法.shiro认证与shiro授权 以下是部分内容,具体见原文. shiro介绍 什么是shiro shiro是Apache ...
- Fiddler怎么可以抓取https的请求包
对于https的协议是带有安全的功能,所有有一些的https的协议是无法抓到的,所以需要通过设置filler中来对,来使用filler的方式的来抓取到https的请求包,那么如何使用filler中抓取 ...
- 《Redis 集群》
由于集群这章节内容较多,也比较重要,所以单独拉出来,做一个小章节. 1:如何搭建一个集群? - 环境为 Ubuntu16.04 - 这里我预计使用 9001 - 9006 端口,生成一个 6 台机器的 ...
- Struts2中.properties文件放置路径(classpath)
一.web应用的classpath简介 classpath路径,即WEB-INF下面的classes目录,所有src目录下面的java.xml.properties等文件编译后都会在此. Stru ...
- 重新安装phpstudy之后出现了403的错误
1. httpd.conf 文件,找到如下代码: DocumentRoot "D:\WWW" <Directory /> Options +Indexes +Follo ...