Auto Layout 的理解

写在前面

iOS的的布局机制「auto layout」不是一个新概念,它早在iOS 6中就推出来了,当下距离iOS 9正式版面世已不远矣,我却对它的了解还比较初级。

我之前对auto layout机制的理解非常粗浅,几乎把它和「constraint」对等。对它的使用有这么几个阶段:

Storyboard中通过拖拽设置constraints;

学习VFL(Visual Format Language)语法使用代码设置constraints;

使用大杀器Masonry

P.S: iOS的VFL语法实在太罗嗦了,又臭又长且可读性差难于调试,无法忍受,Masonry正是解决这一痛点的第三方库,非常好用,学习成本非常低。

近期因为项目,我需要实现一个能够自适应文本自动调整高度的table view cell。网上有相关丰富的资源(category、博客等),思路都非常简单,比如这篇:动态计算UITableViewCell高度详解。但即便如此,我对有些东西理解起来还有障碍,譬如systemLayoutSizeFittingSize:和sizeThatFits:之类的,想到我都将近有大半年的开发经验了,居然还无法理解这么简单的东西,不能忍!

导致这种结果的主要原因有俩:一是之前项目比较简单,涉及auto layout相关的知识无非是add/update/remove constraints;二是自己太轻浮,把auto layout想得太简单;

通过对各种资讯梳理,大概搞明白了自己最大的问题是对auto layout相关的各种API不熟悉或者完全陌生,这导致了无法在一些实际问题中使用正确的策略解决问题。本文的着重点正是结合各种资料加上自己的理解对这些API进行分析。

本文涉及的API包括:

sizeThatFits:和sizeToFit

systemLayoutSizeFittingSize:

intrinsicContentSize

P.S: 这几个API都是与size相关,该如何使用它们呢?这曾让笔者一时非常困惑。以上这几个API都是UIView的实例方法,除此之外,本文还涉及一些属性,譬如preferredMaxLayoutWidth。

iOS布局机制

iOS布局机制大概分这么几个层次:

frame layout

autoresizing

auto layout

frame layout

frame layout最简单直接,简单来说,即通过设置view的frame属性值进而控制view的位置(相对于superview的位置)和大小。

autoresizing

autoresizing和frame layout一样,从一开始存在,它算是后者的补充,基于autoresizing机制,能够让subview和superview维持一定的布局关系,譬如让subview的大小适应superview的大小,随着后者的改变而改变。

站在代码接口的角度来看,autoresizing主要体现在几个属性上,包括(但不限于):

translatesAutoresizingMaskIntoConstraints

autoresizingMask

第一个属性标识view是否愿意被autoresize;

第二个属性是一个枚举值,决定了当superview的size改变时,subview应该做出什么样的调整;

关于autoresizing的更详细使用说明,参考:自动布局之autoresizingMask使用详解

auto layout

autoresizing存在的不足是非常显著的,通过autoresizingMask的可选枚举值可以看出:基于autoresizing机制,我们只能让view在superview的大小改变时做些调整;而无法处理兄弟view之间的关系,譬如处理与兄弟view的间隔;更无法反向处理,譬如让superview依据subview的大小进行调整。

Auto Layout是随着iOS 6推出来的,关于它的介绍,官方文档《Auto Layout Guide》的描述非常精炼:

Auto Layout is a system that lets you lay out your app’s user interface by creating a mathematical description of the relationships between the elements. You define these relationships in terms of constraints either on individual elements, or between sets of elements. Using Auto Layout, you can create a dynamic and versatile interface that responds appropriately to changes in screen size, device orientation, and localization.

简单来说,它是一种基于约束的布局系统,可以根据你在元素(对象)上设置的约束自动调整元素(对象)的位置和大小。

值得一提的是,对于某个view的布局方式,autoresizing和auto layout只能二选一,简单来说,若要对某个view采用auto layout布局,则需要设置其translatesAutoresizingMaskIntoConstraints属性值为NO。

几个重要的API

intrinsicContentSize方法

在介绍intrinsicContentSize方法之前,先来看一个应用场景:

场景一:某个UILabel用于显示单行文本,让其能够自适应文本,即根据文本自动调整其大小。

让UILabel自适应文本,在auto layout之前,一般做法是先给定字体,进而计算文本内容所占据的宽度width和高度height,然后使用得来的width和height设置其frame属性值。

但是使用auto layout非常简单,如下:

@interfaceViewController(){

UILabel*testLabel;

}

- (void)viewDidLoad {

[superviewDidLoad];

self.view.backgroundColor = [UIColorwhiteColor];

testLabel = ({

UILabel*label        = [[UILabelalloc] init];

label.textAlignment  =NSTextAlignmentCenter;

label.font            = [UIFontsystemFontOfSize:14.0];

label.textColor      = [UIColorwhiteColor];

label.backgroundColor = [UIColorlightGrayColor];

label;

});

[self.view addSubview:testLabel];

// 使用Masonry添加constraints

[testLabel mas_makeConstraints:^(MASConstraintMaker *make) {

make.top.equalTo(self.view.mas_top).offset(40);

make.left.equalTo(self.view.mas_left).offset(10);

}];

testLabel.text =@"天地玄黄 宇宙洪荒 日月盈昃 辰宿列张";

}

- (void)viewDidAppear:(BOOL)animated {

[superviewDidAppear:animated];

NSLog(@"testLabel.origin = %@",NSStringFromCGPoint(testLabel.frame.origin));

NSLog(@"testLabel.size = %@",NSStringFromCGSize(testLabel.frame.size));

// print: "testLabel.origin = {10, 40}"

// print: "testLabel.size = {236.5, 17}"

}

效果如下:

问题来了,auto layout system知道testLabel的size呢?

OK,就此引入APIintrinsicContentSize。

intrinsicContentSize是UIView的基础方法(Available in iOS 6.0 and later),UIView Class References对它的描述如下:

Returns the natural size for the receiving view, considering only properties of the view itself.

Return Value

A size indicating the natural size for the receiving view based on its intrinsic properties.

Discussion

Custom views typically have content that they display of which the layout system is unaware. Overriding this method allows a custom view to communicate to the layout system what size it would like to be based on its content. This intrinsic size must be independent of the content frame, because there’s no way to dynamically communicate a changed width to the layout system based on a changed height, for example.

If a custom view has no intrinsic size for a given dimension, it can return UIViewNoIntrinsicMetric for that dimension.

「intrinsic content size」在中文世界里常被译作:「固有内容大小」,简单来说,它被用来告诉auto layout system应该给它分配多大的size

所以呢,在上文代码(场景一的Solution)中,根据我的理解,layout工作流程是这样的:在layout时,auto layout system会去回调testLabel的实例方法intrinsicContentSize,该方法能够根据「文本内容+字体」计算出content的size,进而根据此size对testLabel进行布局。

为了验证这个说法,对上述代码做些改变。

首先定义一个继承自UILabel的类ZWLabel,重写ZWLabel的intrinsicContentSize方法,如下:

@interfaceZWLabel:UILabel

@end

@implementationZWLabel

- (CGSize)intrinsicContentSize {

CGSizesize = [superintrinsicContentSize];

size.width  +=20;

size.height +=20;

returnsize;

}

@end

然后让上文的testLabel从UILabel的实例改为ZWLabel的实例,其余不变:

testLabel = ({

ZWLabel *label = [[ZWLabel alloc] init];

//...

label;

});

效果如下:

效果明显,本示例较为直观说明了intrinsicContentSize这个API的作用了,这个API是为auto layout system的callback提供的!

P.S:笔者刚开始对intrinsicContentSize这个API的用法感到非常疑惑,在我的理解里,它有两种可能:

Auto Layout System会根据content为view设置一个合适的size,开发者有时需要知道这个size,因此可以通过intrinsicContentSize获取;

Auto Layout System在layout时,不知道该为view分配多大的size,因此回调view的intrinsicContentSize方法,该方法会给auto layout system一个合适的size,system根据此size对view的大小进行设置;

现在看来,第二种理解更靠谱!

对于上文所用到的UILabel,想必Cocoa在实现intrinsicContentSize方法时已经根据text属性值和font属性值进行了计算。那是不是每个原生view都实现了intrinsicContentSize呢?

NO!《Auto Layout Guide》在谈论「intrinsic content size」时,总会与另外一个词语「leaf-level views」相关联,譬如:

Intrinsic Content Size

Leaf-level views such as buttons typically know more about what size they should be than does the code that is positioning them. This is communicated through theintrinsic content size, which tells the layout system that a view contains some content that it doesn’t natively understand, and indicates how large that content is, intrinsically.

「leaf-level views」指的是那种一般不包含任何subview的view,譬如UILabel、UIButton等,这类的view往往能够直接计算出content(譬如UILabel的text、UIButton的title,UIImageView的image)的大小。

但是有些view不包含content,譬如UIView,这种view被认为「has no intrinsic size」,它们的intrinsicContentSize返回的值是(-1,-1)。

P.S: 官方文档中说的是:UIView’s default implementation is to return (UIViewNoIntrinsicMetric, UIViewNoIntrinsicMetric),而UIViewNoIntrinsicMetric等于-1,为什么是-1而不是0,我猜是0是一个有效的width/height,而-1不是,更容易区分处理。

还有一种view虽然包含content,但是intrinsicContentSize返回值也是(-1,-1),这类view往往是UIScrollView的子类,譬如UITextView,它们是可滚动的,因此auto layout system在对这类view进行布局时总会存在一些未定因素,Cocoa干脆让这些view的intrinsicContentSize返回(-1,-1)。

preferredMaxLayoutWidth属性

基于上述场景一,我们来分析更复杂一点的UILabel自适应问题。

场景二:某个UILabel用于显示多行文本,让其能够自适应文本,即根据文本自动调整其大小;

对于单行文本UILabel,UILabel的intrinsicContentSize在计算content size时比较容易;但对于多行文本的UILabel,同样的content,譬如「天地玄黄宇宙洪荒」这八个字,摆放方式可以是1x8,可以是2x4,可以是4x2,auto layout system该如何处理呢?UILabel的属性preferredMaxLayoutWidth正是用来应对这个问题的。

UILabel Class References对它的描述如下:

The preferred maximum width (in points) for a multiline label.

Discussion

This property affects the size of the label when layout constraints are applied to it. During layout, if the text extends beyond the width specified by this property, the additional text is flowed to one or more new lines, thereby increasing the height of the label.

preferredMaxLayoutWidth的作用顾名思义,用来限制UILabel content size的最大宽度值。如下代码:

testLabel = ({

UILabel*label                = [[UILabelalloc] init];

label.textAlignment          =NSTextAlignmentCenter;

label.font                    = [UIFontsystemFontOfSize:14.0];

label.textColor              = [UIColorwhiteColor];

label.numberOfLines          =0;// mark

label.preferredMaxLayoutWidth =100;// mark

label.backgroundColor        = [UIColorlightGrayColor];

label;

});

[self.view addSubview:testLabel];

// 使用Masonry添加constraints

[testLabel mas_makeConstraints:^(MASConstraintMaker *make) {

make.top.equalTo(self.view.mas_top).offset(40);

make.left.equalTo(self.view.mas_left).offset(10);

}];

testLabel.text =@"天地玄黄 宇宙洪荒 日月盈昃 辰宿列张";

效果如下:

那么最后testLabel的width是不是就是preferredMaxLayoutWidth的属性值呢?No,最终testLabel的属性值小于等于preferredMaxLayoutWidth的属性值。

sizeThatFits:方法和sizeToFit方法

上文已经提到,UITextView继承自UIScrollView,是可以滚动的,它的intrinsicContentSize方法返回值是(-1,-1),auto layout system在处理UITextView对象时,为其设置的size是(0,0)。如此看来,似乎UITextView无法体验到auto layout带来的好处了。

继续结合应用场景引出sizeThatFits:方法和sizeToFit方法。

场景三:某个UITextView用于显示文本,让其能够自适应文本,即根据文本自动调整其大小;

既然UITextView的content计算方法intrinsicContentSize无法向auto layout system传递我们想要传达的值,我们就应该另想别的方法。

好在iOS有直接的接口可供我们使用。

先谈sizeThatFits:方法,UIView Class References对它的描述如下:

Asks the view to calculate and return the size that best fits the specified size.

Return Value

A new size that fits the receiver’s subviews.

Discussion

The default implementation of this method returns the existing size of the view. Subclasses can override this method to return a custom value based on the desired layout of any subviews. For example, a UISwitch object returns a fixed size value that represents the standard size of a switch view, and a UIImageView object returns the size of the image it is currently displaying.

简单来说,调用sizeThatFits:方法意味着「根据文本计算最适合UITextView的size」。从功能来讲,sizeThatFits:和intrinsicContentSize方法比较类似,都是用来计算view的size的。笔者曾一度对二者的关系非常疑惑,甚至觉得二者存在相互调用的关系。后来通过验证发现不是这么回事儿,后文会通过示例说明。

对于显示多行文本的UILabel,为了方便intrinsicContentSize方法更方便计算content size,需要指定preferredMaxLayoutWidth属性值;对于UITextView的sizeThatFits:,似乎有类似的需求,毕竟UITextView也可能会显示多行啊,这样说来,UITextView也有一个preferredMaxLayoutWidth属性?

No!preferredMaxLayoutWidth属性是iOS 6才引入的,sizeThatFits:方法则早得多,况且,UITextView是可以滚动的,哪怕文本不会全部呈现出来,但也可以通过左右或者上下滚动浏览所有内容;传给sizeThatFits:的参数(假设为size)是CGSize类型,size.width的功能和UILabel的preferredMaxLayoutWidth差不多,指定了UITextView区域的最大宽度,size.height则指定了UITextView区域的最大高度;可能有人问,若传给sizeThatFits:的size小于UITextView的text面积怎么办,岂不是有些内容无法显示出来?傻啊,可以滚啊!

值得一提的是,调用sizeThatFits:并不改变view的size,它只是让view根据已有content和给定size计算出最合适的view.size。

那么sizeToFit方法是干嘛的呢?很简单:

calls sizeThatFits: with current view bounds and changes bounds size.

P.S:有点不太理解,这个「current view」指的是啥?self?还是superview?

P.P.S:经过验证,这里的「current view」指的是self。简单来说,sizeToFit等价于:

// calls sizeThatFits

CGSizesize = [selfsizeThatFits:self.bounds.size];

// change bounds size

CGRectbounds =self.bounds;

bounds.size.width = size.width;

bounds.size.height = size.width;

self.bounds = bounds;

P.S:值得一提的是,经过测试发现,当调用sizeThatFits:的size=(width, height),当width/height的值为0时,width/height似乎就被认为是无穷大!

systemLayoutSizeFittingSize:方法

首先来看一个应用场景。

场景四:某个UIView,宽度等于屏幕宽度,包含两个UILabel,两个label都可能显示多行文本,要求结合auto layout让UIView大小能够自适应subviews。

Easy,给出如下代码:

- (void)viewDidLoad {

[superviewDidLoad];

self.view.backgroundColor = [UIColorlightGrayColor];

bgView = ({

UIView*view        = [[UIViewalloc] init];

view.backgroundColor = [UIColorgrayColor];

view;

});

[self.view addSubview:bgView];

label1 = ({

UILabel*label                = [[UILabelalloc] init];

label.font                    = [UIFontsystemFontOfSize:14.0];

label.preferredMaxLayoutWidth =self.view.frame.size.width-20;

label.numberOfLines          =0;

label.textColor              = [UIColorwhiteColor];

label.backgroundColor        = [UIColorpurpleColor];

label;

});

[bgView addSubview:label1];

label2 = ({

UILabel*label                = [[UILabelalloc] init];

label.font                    = [UIFontsystemFontOfSize:18.0];

label.preferredMaxLayoutWidth =self.view.frame.size.width-20;

label.numberOfLines          =0;

label.textColor              = [UIColorwhiteColor];

label.backgroundColor        = [UIColorredColor];

label;

});

[bgView addSubview:label2];

// 添加约束(基于Masonry)

[bgView mas_makeConstraints:^(MASConstraintMaker *make) {

make.left.equalTo(self.view.mas_left);

make.top.equalTo(self.view.mas_top).offset(10);

make.width.equalTo(self.view.mas_width);

}];

[label1 mas_makeConstraints:^(MASConstraintMaker *make) {

make.left.equalTo(bgView.mas_left).offset(10);

make.right.lessThanOrEqualTo(bgView.mas_right).offset(-10);

make.top.equalTo(bgView.mas_top).offset(10);

}];

[label2 mas_makeConstraints:^(MASConstraintMaker *make) {

make.left.equalTo(label1.mas_left);

make.right.lessThanOrEqualTo(bgView.mas_right).offset(-10);

make.top.equalTo(label1.mas_bottom).offset(10);

make.bottom.equalTo(bgView.mas_bottom).offset(-10);

}];

label1.text =@"天地玄黄 宇宙洪荒 日月盈昃 辰宿列张 寒来暑往 秋收冬藏 闰余成岁 律吕调阳";

label2.text =@"天地玄黄 宇宙洪荒 日月盈昃 辰宿列张 寒来暑往 秋收冬藏 闰余成岁 律吕调阳";

}

代码看得有些枯燥,简单来说,bgView(UIView)中嵌入两个能显示多行文本的label1(UILabel)和label2(UILabel),设置约束如下:

代码运行后的显示效果:

代码中除了添加各种各样的constraints,没有任何设置frame的代码,显然都是基于auto layout的。

那么问题来了,理解label1和label2的布局没啥子问题,因为它们的intrinsicContentSize方法会将content size告诉auto layout system,进而后者会为它们的size设置对应值;但对于bgView,它可是一个UIView对象,它的intrinsicContentSize回调方法的返回值为(-1,-1),那么auto layout system是如何为它设置合适的size的呢?

根据我的理解,auto layout system在处理某个view的size时,参考值包括:

自身的intrinsicContentSize方法返回值;

subviews的intrinsicContentSize方法返回值;

自身和subviews的constraints;

OK,根据笔者理解,结合上图,我认为auto layout system是这样计算一下bgView的size的:

width=max{10+size1.width+10, 10+size2.width+10, size3.width}

height=max{10+size1.height+10+size2.height+10, size3.height}

我们在viewDidAppear:方法中将相关值打印出来瞧瞧看:

- (void)viewDidAppear:(BOOL)animated {

[superviewDidAppear:animated];

CGSizesize1 = [label1 intrinsicContentSize];

CGSizesize2 = [label2 intrinsicContentSize];

CGSizesize3 = [bgView intrinsicContentSize];

NSLog(@"size1 = %@",NSStringFromCGSize(size1));// print: "size1 = {300, 33.5}"

NSLog(@"size2 = %@",NSStringFromCGSize(size2));// print: "size2 = {290.5, 64.5}"

NSLog(@"size3 = %@",NSStringFromCGSize(size3));// print: "size3 = {-1, -1}"

CGSizebgViewSize = bgView.frame.size;

NSLog(@"bgViewSize = %@",NSStringFromCGSize(bgViewSize));// print: "bgViewSize = {320, 128}"

}

完全吻合我理解的auto layout size计算公式。

P.S:然而,我知道,事实往往并没有这么简单,当处理自定义View时,当constraints设置不完整或者冲突时,事情总会变得复杂起来,也总会得到意想不到的结果。但,暂且就这么理解吧!

啰里啰嗦写了这么多,还没引出systemLayoutSizeFittingSize:方法…

OK,再来看另外一个应用场景。

场景五:某个UIView,宽度等于屏幕宽度,包含一个UILabel和一个UITextView,二者都可能显示多行文本,要求结合auto layout让UIView大小能够自适应subviews。

在场景四代码基础上将label2改为UITextView对象textView1,如下:

- (void)viewDidLoad {

[superviewDidLoad];

self.view.backgroundColor = [UIColorlightGrayColor];

bgView = ({

UIView*view        = [[UIViewalloc] init];

view.backgroundColor = [UIColorgrayColor];

view;

});

[self.view addSubview:bgView];

label1 = ({

UILabel*label                = [[UILabelalloc] init];

label.font                    = [UIFontsystemFontOfSize:14.0];

label.preferredMaxLayoutWidth =self.view.frame.size.width-20;

label.numberOfLines          =0;

label.textColor              = [UIColorwhiteColor];

label.backgroundColor        = [UIColorpurpleColor];

label;

});

[bgView addSubview:label1];

textView1 = ({

UITextView*label    = [[UITextViewalloc] init];

label.font            = [UIFontsystemFontOfSize:18.0];

label.textColor      = [UIColorwhiteColor];

label.backgroundColor = [UIColorredColor];

label;

});

[bgView addSubview:textView1];

// 添加约束(基于Masonry)

[bgView mas_makeConstraints:^(MASConstraintMaker *make) {

make.left.equalTo(self.view.mas_left);

make.top.equalTo(self.view.mas_top).offset(10);

make.width.equalTo(self.view.mas_width);

}];

[label1 mas_makeConstraints:^(MASConstraintMaker *make) {

make.left.equalTo(bgView.mas_left).offset(10);

make.right.lessThanOrEqualTo(bgView.mas_right).offset(-10);

make.top.equalTo(bgView.mas_top).offset(10);

}];

[textView1 mas_makeConstraints:^(MASConstraintMaker *make) {

make.left.equalTo(label1.mas_left);

make.right.lessThanOrEqualTo(bgView.mas_right).offset(-10);

make.top.equalTo(label1.mas_bottom).offset(10);

make.bottom.equalTo(bgView.mas_bottom).offset(-10);

}];

label1.text =@"天地玄黄 宇宙洪荒 日月盈昃 辰宿列张 寒来暑往 秋收冬藏 闰余成岁 律吕调阳";

textView1.text =@"天地玄黄 宇宙洪荒 日月盈昃 辰宿列张 寒来暑往 秋收冬藏 闰余成岁 律吕调阳";

}

运行效果如下:

这显然不是我们想要的结果,至少存在这么两个问题:

textView1不见了;

bgView的大小不是我们想要的;

为什么会有出现这样的问题呢?

首先正如上文所提到的那样,textView1是UITextView的对象,而UITextView是UIScrollView的子类,它的intrinsicContentSize:方法的返回值是(-1,-1),这意味着textView1对bgView的auto layout size没有产生影响。

那textView1为什么不见了呢?或者说,为什么textView1的size为CGSizeZero呢?根据我对auto layout system的理解,auto layout system在处理view的size时,受三个因素影响:

自身的intrinsicContentSize方法返回值;

subviews的intrinsicContentSize方法返回值;

自身和subviews的constraints;

对于textView1,前两个因素可以忽略掉,简而言之,textView1的size由它自身的constraints决定。而根据上述代码的约束可以计算出textView1的height:

height = bgView.height-10-label1.height-10-10;

而bgView.height=10+label1.height+10+10;意味着textView1的height值为0;当然就看不到了textView1了。

因此,若想要让textView1正常可见,至少有这么一种策略:直接为textView1添加约束设置width和height;

简单来说,override回调方法viewWillLayoutSubviews,如下:

- (void)viewWillLayoutSubviews {

[superviewWillLayoutSubviews];

CGSizetextViewFitSize = [textView1 sizeThatFits:CGSizeMake(self.view.frame.size.width-20,0)];

[textView1 mas_updateConstraints:^(MASConstraintMaker *make) {

make.width.equalTo([NSNumbernumberWithFloat:textViewFitSize.width]);

make.height.equalTo([NSNumbernumberWithFloat:textViewFitSize.height]);

}];

}

这种做法是有效的,运行效果如下:

若将这种场景切换到table view cell中会如何呢?简单来说,如果将上述的bgView换成UITableViewCell(或其子类)对象又会如何呢?

在table view中,我们可以使用- (CGFloat)tableView:heightForRowAtIndexPath:接口,该回调方法会返回CGFloat值,该值指示了对应cell的高度;假设auto layout system为cell分配的size是autoSize,在处理返回值时额外加上textViewFitSize.height即可。

但问题是,我们如何获取这个autoSize的值呢?毕竟此时cell还未布局完成啊,直接读取cell.frame.size肯定是不行的。

systemLayoutSizeFittingSize:方法正是用于处理这个问题的。

UIView Class References对该方法描述如下:

Returns the size of the view that satisfies the constraints it holds.

Return Value

The size of the view that satisfies the constraints it holds.

Discussion

Determines the best size of the view considering all constraints it holds and those of its subviews.

我是这么理解systemLayoutSizeFittingSize:的:对于使用auto layout机制布局的view,auto layout system会在布局过程中综合各种约束的考虑为之设置一个size,在布局完成后,该size的值即为view.frame.size的值;这包含的另外一层意思,即在布局完成前,我们是不能通过view.frame.size准确获取view的size的。但有时候,我们需要在auto layout system对view完成布局前就知道它的size,systemLayoutSizeFittingSize:方法正是能够满足这种要求的API。systemLayoutSizeFittingSize:方法会根据其constraints返回一个合适的size值。

systemLayoutSizeFittingSize:方法可传入一个参数,目前有两个值可以传入:

UILayoutFittingCompressedSize : The option to use the smallest possible size.

UILayoutFittingExpandedSize : The option to use the largest possible size.

值得一提的是,在使用[view systemLayoutSizeFittingSize:]时,要注意尽量确保view的constraints的完整性,这样参数UILayoutFittingCompressedSize和UILayoutFittingExpandedSize得到的结果是一样的。否则,举个例子,若view的right属性没有设置,则这两个参数得到systemLayoutSizeFittingSize:返回值size是不一样的,前者size.width=0,后者size.width=1000。

P.S:这纯属个人使用体验。

至于systemLayoutSizeFittingSize:的使用场景,动态计算UITableViewCell高度详解非常值得参考!

对比几种API

在刚开始接触这几个API时感到非常困惑。分不清intrinsicContentSize、sizeThatFits:以及systemLayoutSizeFittingSize:的区别。经过这么将近一天的折腾,现在大概有了基本的判断。

首先说intrinsicContentSize,它的最主要作用是告诉auto layout system一些信息,可以认为它是后者的回调方法,auto layout system在对view进行布局时会参考这个回调方法的返回值;一般很少像CGSize size = [view intrinsicContentSize]去使用intrinsicContentSizeAPI。

再来看sizeThatFits:和systemLayoutSizeFittingSize:,它们俩非常相似,都是为开发者直接服务的API(而不是回调方法)。所不同的是,sizeThatFits:是auto layout之前就存在的,一般在leaf-level views中用得比较多,在计算size过程中,它可不会考虑constraints神马的;对于systemLayoutSizeFittingSize:,它是随着auto layout(iOS 6)引入的,用于在view完成布局前获取size值,如果view的constraints确保了完整性和正确性,通常它的返回值就是view完成布局之后的view.frame.size的值。

它们之前存在相互调用的关系吗?经过测试发现,三者之前没有直接的调用关系。但是能得出这样的结论:intrinsicContentSize的返回值会直接影响systemLayoutSizeFittingSize:的返回值。至于底层是如何处理的不得而知。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 156,907评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,546评论 1 289
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,705评论 0 238
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,624评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,940评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,371评论 1 210
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,672评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,396评论 0 195
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,069评论 1 238
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,350评论 2 242
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,876评论 1 256
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,243评论 2 251
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,847评论 3 231
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,004评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,755评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,378评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,266评论 2 259

推荐阅读更多精彩内容

  • (一)Masonry介绍 Masonry是一个轻量级的布局框架 拥有自己的描述语法 采用更优雅的链式语法封装自动布...
    木易林1阅读 2,197评论 0 3
  • Masonry是一个轻量级的布局框架,拥有自己的描述语法,采用更优雅的链式语法封装自动布局,简洁明了并具有高可读性...
    3dcc6cf93bb5阅读 1,680评论 0 1
  • iOS_autoLayout_Masonry 概述 Masonry是一个轻量级的布局框架与更好的包装AutoLay...
    指尖的跳动阅读 1,120评论 1 4
  • 原文网址:http://www.cnblogs.com/dingyufenglian/p/4845477.html...
    chen1zee阅读 9,057评论 0 9
  • 30岁,感觉我的人生已经走完。 我的孩子,如果你出生我会尽心尽力去照顾你但不会溺爱你!在你很小的时候就要好好教育你...
    金令幽兰阅读 285评论 0 1