AutoLayout框架Masonry使用心得
AutoLayout框架Masonry使用心得
我们组分享会上分享了页面布局的一些写法,中途提到了AutoLayout,会后我决定将很久前挖的一个坑给填起来(还有好多坑就不说了,说了不填更毁形象了)。
可使用的框架首推Masonry,关于为啥选择Masonry看看官方文档就明白了https://github.com/SnapKit/Masonry,官方称AutoLayout所有功能Masonry都支持。这次项目界面方面我就全部使用了Masonry。
AutoLayout的一些基本概念
- 利用约束来控制视图的大小和位置,系统会在运行时通过设置的约束计算得到frame再绘制屏幕
- 两个属性Content Compression Resistance(排挤,值越高越固定)和Content Hugging(拥抱),Masonry代码如下
//content hugging 为1000
[view setContentHuggingPriority:UILayoutPriorityRequired
forAxis:UILayoutConstraintAxisHorizontal];
//content compression 为250
[view setContentCompressionResistancePriority:UILayoutPriorityDefaultLow
forAxis:UILayoutConstraintAxisHorizontal];
- multipler属性表示约束值为约束对象的百分比,在Masonry里有对应的multipliedBy函数
//宽度为superView宽度的20%
make.width.equalTo(superView.mas_width).multipliedBy(0.2);
- AutoLayout下UILabel设置多行计算需要设置preferredMaxLayoutWidth
label.preferredMaxWidth = [UIScreen mainScreen].bounds.size.width - margin - padding;
- preferredMaxLayoutWidth用来制定最大的宽,一般用在多行的UILabel中
- systemLayoutSizeFittingSize方法能够获得view的高度
- iOS7有两个很有用的属性,topLayoutGuide和bottomLayoutGuide,这个两个主要是方便获取UINavigationController和UITabBarController的头部视图区域和底部视图区域。
//Masonry直接支持这个属性
make.top.equalTo(self.mas_topLayoutGuide);
AutoLayout关于更新的几个方法的区别
- setNeedsLayout:告知页面需要更新,但是不会立刻开始更新。执行后会立刻调用layoutSubviews。
- layoutIfNeeded:告知页面布局立刻更新。所以一般都会和setNeedsLayout一起使用。如果希望立刻生成新的frame需要调用此方法,利用这点一般布局动画可以在更新布局后直接使用这个方法让动画生效。
- layoutSubviews:系统重写布局
- setNeedsUpdateConstraints:告知需要更新约束,但是不会立刻开始
- updateConstraintsIfNeeded:告知立刻更新约束
- updateConstraints:系统更新约束
Masonry使用注意事项
- 用mas_makeConstraints的那个view需要在addSubview之后才能用这个方法
- mas_equalTo适用数值元素,equalTo适合多属性的比如make.left.and.right.equalTo(self.view)
- 方法and和with只是为了可读性,返回自身,比如make.left.and.right.equalTo(self.view)和make.left.right.equalTo(self.view)是一样的。
- 因为iOS中原点在左上角所以注意使用offset时注意right和bottom用负数。
Masonry适配iOS6和iOS7时需要注意的问题
开发项目时是先在iOS8上调试完成的,测试时发现低版本的系统会发生奔溃的现象,修复后总结问题主要是在equalTo的对象指到了父视图的父视图或者父视图同级的子视图上造成的,所以做约束时如果出现了奔溃问题百分之九十都是因为这个。
Masonry使用范例
基本写法
//相对于父视图边距为10
UIEdgeInsets padding = UIEdgeInsetsMake(10, 10, 10, 10);
[self.avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(superview.mas_top).with.offset(padding.top); //with is an optional semantic filler
make.left.equalTo(superview.mas_left).with.offset(padding.left);
make.bottom.equalTo(superview.mas_bottom).with.offset(-padding.bottom);
make.right.equalTo(superview.mas_right).with.offset(-padding.right);
}];
//相对于父视图边距为10简洁写法
[self.avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(superview).with.insets(padding);
}];
//这两个作用完全一样
[self.avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.greaterThanOrEqualTo(self.view);
make.left.greaterThanOrEqualTo(self.view.mas_left);
}];
//.equalTo .lessThanOrEqualTo .greaterThanOrEqualTo使用NSNumber
[self.avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.greaterThanOrEqualTo(@200);
make.width.lessThanOrEqualTo(@400);
make.left.lessThanOrEqualTo(@10);
}];
//如果不用NSNumber可以用以前的数据结构,只需用mas_equalTo就行
[self.avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(42);
make.height.mas_equalTo(20);
make.size.mas_equalTo(CGSizeMake(50, 100));
make.edges.mas_equalTo(UIEdgeInsetsMake(10, 0, 10, 0));
make.left.mas_equalTo(self.view).mas_offset(UIEdgeInsetsMake(10, 0, 10, 0));
}];
//也可以使用数组
[self.avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
make.height.equalTo(@[self.view.mas_height, superview.mas_height]);
make.height.equalTo(@[self.view, superview]);
make.left.equalTo(@[self.view, @100, superview.mas_right]);
}];
// priority的使用
[self.avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.greaterThanOrEqualTo(self.view.mas_left).with.priorityLow();
make.top.equalTo(self.view.mas_top).with.priority(600);
}];
//同时创建多个约束
[self.avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
//让top,left,bottom,right都和self.view一样
make.edges.equalTo(self.view);
//edges
make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(5, 10, 15, 20));
//size
make.size.greaterThanOrEqualTo(self.view);
make.size.equalTo(superview).sizeOffset(CGSizeMake(100, -50));
//center
make.center.equalTo(self.view);
make.center.equalTo(self.view).centerOffset(CGPointMake(-5, 10));
//chain
make.left.right.and.bottom.equalTo(self.view);
make.top.equalTo(self.view);
}];
AutoLayout情况如何计算UITableView的变高高度
主要是UILabel的高度会有变化,所以这里主要是说说label变化时如何处理,设置UILabel的时候注意要设置preferredMaxLayoutWidth这个宽度,还有ContentHuggingPriority为UILayoutPriorityRequried
CGFloat maxWidth = [UIScreen mainScreen].bounds.size.width - 10 * 2;
textLabel = [UILabel new];
textLabel.numberOfLines = 0;
textLabel.preferredMaxLayoutWidth = maxWidth;
[self.contentView addSubview:textLabel];
[textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(statusView.mas_bottom).with.offset(10);
make.left.equalTo(self.contentView).with.offset(10);
make.right.equalTo(self.contentView).with.offset(-10);
make.bottom.equalTo(self.contentView).with.offset(-10);
}];
[_contentLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
如果版本支持最低版本为iOS 8以上的话可以直接利用UITableViewAutomaticDimension在tableview的heightForRowAtIndexPath直接返回即可。
tableView.rowHeight = UITableViewAutomaticDimension;
tableView.estimatedRowHeight = 80; //减少第一次计算量,iOS7后支持
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// 只用返回这个!
return UITableViewAutomaticDimension;
}
但如果需要兼容iOS 8之前版本的话,就要回到老路子上了,主要是用systemLayoutSizeFittingSize来取高。步骤是先在数据model中添加一个height的属性用来缓存高,然后在table view的heightForRowAtIndexPath代理里static一个只初始化一次的Cell实例,然后根据model内容填充数据,最后根据cell的contentView的systemLayoutSizeFittingSize的方法获取到cell的高。具体代码如下
//在model中添加属性缓存高度
@interface DataModel : NSObject
@property (copy, nonatomic) NSString *text;
@property (assign, nonatomic) CGFloat cellHeight; //缓存高度
@end
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
static CustomCell *cell;
//只初始化一次cell
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([CustomCell class])];
});
DataModel *model = self.dataArray[(NSUInteger) indexPath.row];
[cell makeupData:model];
if (model.cellHeight <= 0) {
//使用systemLayoutSizeFittingSize获取高度
model.cellHeight = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height + 1;
}
return model.cellHeight;
}
动画
因为布局约束就是要脱离frame这种表达方式的,可是动画是需要根据这个来执行,这里面就会有些矛盾,不过根据前面说到的布局约束的原理,在某个时刻约束也是会被还原成frame使视图显示,这个时刻可以通过layoutIfNeeded这个方法来进行控制。具体代码如下
[aniView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.bottom.left.right.equalTo(self.view).offset(10);
}];
[aniView mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view).offset(30);
}];
[UIView animateWithDuration:3 animations:^{
[self.view layoutIfNeeded];
}];
AutoLayout框架Masonry使用心得的更多相关文章
- iOS 自动布局框架 – Masonry 详解
目前iOS开发中大多数页面都已经开始使用Interface Builder的方式进行UI开发了,但是在一些变化比较复杂的页面,还是需要通过代码来进行UI开发的.而且有很多比较老的项目,本身就还在采用纯 ...
- iOS自动布局框架-Masonry详解
首先,在正式使用Masonry之前,我们先来看看在xib中我们是如何使用AutoLayout 从图中我们可以看出,只要设置相应得局限,控制好父视图与子视图之间的关系就应该很ok的拖出你需要的需 ...
- 使用第三方框架 Masonry 实现自动布局
使用第三方框架 Masonry 实现自动布局 时间:2015-02-10 11:08:41 阅读:4595 评论:0 收藏:0 [点我收藏+] 标签: 由于前两 ...
- Android 数据库ORM框架GreenDao学习心得及使用总结<一>
转: http://www.it165.net/pro/html/201401/9026.html 最近在对开发项目的性能进行优化.由于项目里涉及了大量的缓存处理和数据库运用,需要对数据库进行频繁的读 ...
- iOS开发通过代码方式使用AutoLayout (NSLayoutConstraint + Masonry)
iOS开发通过代码方式使用AutoLayout (NSLayoutConstraint + Masonry) 随着iPhone6/6+设备的上市,如何让手头上的APP适配多种机型多种屏幕尺寸变得尤为迫 ...
- 【转载】Android开源:数据库ORM框架GreenDao学习心得及使用总结
转载链接:http://www.it165.net/pro/html/201401/9026.html 最近在对开发项目的性能进行优化.由于项目里涉及了大量的缓存处理和数据库运用,需要对数据库进行频繁 ...
- iOS — Autolayout之Masonry解读
前言 1 MagicNumber -> autoresizingMask -> autolayout 以上是纯手写代码所经历的关于页面布局的三个时期 在iphone1-iphone3gs时 ...
- ios开发学习笔记040-autolayout 第三方框架Masonry
不管是是界面创建约束还是代码创建约束,苹果官方提供的方式都比较繁琐.所以出现了第三方框架. Masonry 在github地址如下: https://github.com/SnapKit/Masonr ...
- 代码方式使用AutoLayout (NSLayoutConstraint + Masonry)
随着iPhone6/6+设备的上市,如何让手头上的APP适配多种机型多种屏幕尺寸变得尤为迫切和必要.(包括:iPhone4/4s,iPhone5/5s,iPhone6/6s,iPhone 6p/6ps ...
随机推荐
- VS2013与MySql建立连接;您的项目引用了最新实体框架;但是,找不到数据链接所需的与版本兼容的实体框架数据库 EF6使用Mysql的技巧
因为之前都是看别人的项目,而没有自己从头到尾建立一个项目,所以这次尝试搭建时就出现了问题,主要是ASP.Net MVC项目中VS2013和MySql的连接. 第一个问题: 数据库表已建好,相应的数据库 ...
- [itint5]树中最大路径和
http://www.itint5.com/oj/#13 要注意,一是空路径也可以,所以最小是0.然后要时刻注意路径顶多有两条子路径+根节点组成,所以更新全局最值时和返回上一级的值要注意分清. #in ...
- thinkphp 分组
分组 配置项: // 开启分组 'APP_GROUP_LIST'=>'Home,Admin', // 默认分组 'DEFAULT_GROUP'=>'Home', 涉及分组: 配置文件分组, ...
- 【转】linux C++ 获取文件信息 stat函数详解
stat函数讲解 表头文件: #include <sys/stat.h> #include <unistd.h>定义函数: int stat ...
- MyBatis的分页操作(MySQL)
1.无条件分页: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC ...
- [转]Linux下Nagios的安装与配置
转自:http://blog.chinaunix.net/uid-29539073-id-4149856.html 月色书香 一.Nagios简介 Nagios是一款开源的电脑系统和网络监视工具,能有 ...
- python auto send email
/*************************************************************************** * python auto send emai ...
- apache开源项目--HttpComponents
HttpComponents 也就是以前的httpclient项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端/服务器编程工具包,并且它支持 HTTP 协议最新的版本和建议.不 ...
- 【转】UINavigationBar 使用总结
原文网址:http://www.jianshu.com/p/f0d3df54baa6 UINavigationBar是我们在开发过程中经常要用到的一个控件,下面我会为大家介绍一些常用的用法. 1. 设 ...
- ubuntu桌面右上角键盘图标不见解决方法
今天出现了这个问题,桌面右上角的键盘图标不见,找到解决方法如下: 打开终端,分别输入以下命令即可: killall ibus-daemon 这个表示结束进程 ibus-daemon -d 这个表示重启 ...