下面列出View的layout的方法:
layoutSubviews
layoutIfNeeded
setNeedsLayout
setNeedsDisplay
drawRect
sizeThatFits
sizeToFit
下面進(jìn)行驗(yàn)證
-
1.layoutSubviews
先來看看蘋果官方文檔
| Description |
Lays out subviews.
The default implementation of this method does nothing on iOS 5.1 and earlier. Otherwise, the default
implementation uses any constraints you have set to determine the size and position of any subviews.
Subclasses can override this method as needed to perform more precise layout of their subviews. You should
override this method only if the autoresizing and constraint-based behaviors of the subviews do not offer the behavior you want. You can use your
implementation to set the frame rectangles of your subviews directly.
You should not call this method directly. If you want to force a layout update, call the [setNeedsLayout](apple-
reference-documentation:/hcKxHs2z9R) method instead to do so prior to the next drawing update. If you want to
update the layout of your views immediately, call the [layoutIfNeeded](apple-reference-documentation://hcR6Nt0dCa) method.
-------------------------------------------------------------------------------------------------------------------------
翻譯
此方法的默認(rèn)實(shí)現(xiàn)在iOS 5.1及更早版本中不執(zhí)行任何操作。 否則温治,默認(rèn)
實(shí)現(xiàn)使用您設(shè)置的任何約束來確定任何子視圖的大小和位置像屋。
子類可以根據(jù)需要覆蓋此方法,以執(zhí)行其子視圖的更精確布局哨苛。 你應(yīng)該
僅當(dāng)子視圖的自動(dòng)調(diào)整和基于約束的行為不提供所需的行為時(shí)才覆蓋此方法。 你可以使用你的
實(shí)現(xiàn)直接設(shè)置子視圖的框架矩形币砂。
您不應(yīng)該直接調(diào)用此方法建峭。 如果要強(qiáng)制進(jìn)行布局更新,請(qǐng)調(diào)用setNeedsLayout方法改為在下次繪圖更新之前執(zhí)行此操作决摧。 如果你想立即更新視圖的
布局亿蒸,調(diào)用layoutIfNeeded方法凑兰。
這個(gè)方法本身不會(huì)做任何事情,需要在子類中重寫边锁,那么姑食,什么時(shí)候會(huì)觸發(fā)此方法呢?
1.直接調(diào)用[self setNeedsLayout];(這個(gè)在上面蘋果官方文檔里有說明)
2.addSubview時(shí)
3.view的frame的值設(shè)置前后發(fā)生了變化
4.滑動(dòng)UIScrollView的時(shí)
5.旋轉(zhuǎn)Screen會(huì)觸發(fā)父UIView上的layoutSubviews事件(有人說旋轉(zhuǎn)時(shí)不會(huì)觸發(fā)茅坛,我這里試了模擬器和真機(jī)都是可以的)
1.自定義一個(gè)view繼承UIView
@implementation TestView
- (void)layoutSubviews{
NSLog(@"heloo");
}
2.初始化添加到view上
#import "ViewController.h"
#import "TestView.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
TestView * test = [[TestView alloc]init];
[self.view addSubview:test];
}
可以看到觸發(fā)了方法屏幕快照 2018-10-10 下午2.36.31.png
frame在發(fā)生改變時(shí)也會(huì)觸發(fā)音半,這里觸發(fā)了兩次
屏幕快照 2018-10-10 下午2.38.11.png
-
2. layoutIfNeeded
使用約束的時(shí)候 調(diào)一下可以立即更新效果
setNeedsLayout方法并不會(huì)立即刷新,立即刷新需要調(diào)用layoutIfNeeded方法贡蓖!
-
3. setNeedsDisplay
與setNeedsLayout方法相似的方法是setNeedsDisplay方法曹鸠。該方法在調(diào)用時(shí),會(huì)自動(dòng)調(diào)用drawRect方法斥铺。drawRect方法主要用來畫圖彻桃。所以,當(dāng)需要刷新布局時(shí)晾蜘,用setNeedsLayOut方法邻眷;當(dāng)需要重新繪畫時(shí),調(diào)用setNeedsDisplay方法剔交。
-
4. sizeThatFits肆饶、sizeToFit
在使用UILabel的時(shí)候會(huì)用到,使用這兩個(gè)方法之前省容,必須要給label賦值抖拴,否則不會(huì)顯示內(nèi)容
[testLabel sizeThatFits:CGSizeMake(20, 20)];//會(huì)計(jì)算出最優(yōu)的 size 但是不會(huì)改變 自己的 size,個(gè)人認(rèn)為這個(gè)就是 label 自適應(yīng)大小有用別的沒什么用
[testLabel sizeToFit];//會(huì)計(jì)算出最優(yōu)的 size 而且會(huì)改變自己的size