在做自定義cell的時(shí)候桨仿,我一般會(huì)在LayoutSubView和UpdateConstraint中設(shè)置子view的frame湿痢,但是多多少少都會(huì)在這兩個(gè)方法里面遇見(jiàn)坑慕爬。這次寫(xiě)下來(lái)只是為了做個(gè)對(duì)比膀斋。
以自定義View內(nèi)調(diào)用這兩個(gè)方法作對(duì)比
#import "TestView.h"
@interface TestView()
@property (nonatomic, assign) NSInteger count;
@end
@implementation TestView
//- (instancetype)init {
// if (self = [super init]) {
//// self.frame = CGRectMake(0, 0, 200, 200);
//// self.backgroundColor = [UIColor redColor];
// }
// return self;
//}
- (void)layoutSubviews {
_count++;
NSLog(@"初始化調(diào)用了%ld次",_count);
[super layoutSubviews];
}
#import "ViewController.h"
#import "TestView.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:[TestView new]];
// Do any additional setup after loading the view, typically from a nib.
}
@end
結(jié)果是
2018-10-22 11:21:48.202205+0800 TestFrame[18201:428360] 初始化調(diào)用了1次
如果重寫(xiě)了init方法
- (instancetype)init {
if (self = [super init]) {
// self.frame = CGRectMake(0, 0, 200, 200);
// self.backgroundColor = [UIColor redColor];
}
return self;
}
- (void)layoutSubviews {
_count++;
NSLog(@"初始化調(diào)用了%ld次",_count);
[super layoutSubviews];
}
結(jié)局還是一樣
2018-10-22 11:27:35.208790+0800 TestFrame[18375:450111] 初始化調(diào)用了1次
當(dāng)然設(shè)置背景顏色也是調(diào)用了1次
添加了self.frame = CGRectMake(0, 0, 200, 200);
結(jié)果是
2018-10-22 13:43:16.873562+0800 TestFrame[21976:564028] 初始化調(diào)用了1次
2018-10-22 13:43:16.873702+0800 TestFrame[21976:564028] 初始化調(diào)用了2次
將屏幕旋轉(zhuǎn)會(huì)再調(diào)用一次
添加一個(gè)子View
- (instancetype)init {
if (self = [super init]) {
self.frame = CGRectMake(0, 0, 200, 200);
self.backgroundColor = [UIColor redColor];
UIImageView *sub = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 40)];
sub.backgroundColor = [UIColor yellowColor];
[self addSubview:sub];
}
return self;
}
- (void)layoutSubviews {
_count++;
NSLog(@"初始化調(diào)用了%ld次",_count);
[super layoutSubviews];
}
結(jié)果是調(diào)用了2次
2018-10-22 13:53:51.607028+0800 TestFrame[22279:597161] 初始化調(diào)用了1次
2018-10-22 13:53:51.607176+0800 TestFrame[22279:597161] 初始化調(diào)用了2次
那如果添加多個(gè)子View呢
- (instancetype)init {
if (self = [super init]) {
self.frame = CGRectMake(0, 0, 200, 200);
self.backgroundColor = [UIColor redColor];
for (NSInteger i = 0; i < 3; i++) {
UIImageView *sub = [[UIImageView alloc] initWithFrame:CGRectMake(i * 30, 0, 30, 40)];
sub.backgroundColor = [UIColor yellowColor];
[self addSubview:sub];
}
UILabel *ss = [[UILabel alloc] initWithFrame:CGRectMake(0, 60, 30, 30)];
ss.text = @"sss";
ss.backgroundColor = [UIColor blueColor];
[self addSubview:ss];
}
return self;
}
- (void)layoutSubviews {
_count++;
NSLog(@"初始化調(diào)用了%ld次",_count);
[super layoutSubviews];
}
結(jié)果還是
2018-10-22 13:57:54.479344+0800 TestFrame[22416:610776] 初始化調(diào)用了1次 2018-10-22 13:57:54.479660+0800 TestFrame[22416:610776] 初始化調(diào)用了2次
假如在View繼承于ScorllView似将,在滾動(dòng)的時(shí)候便會(huì)不斷調(diào)用layoutSubView
總結(jié):
layoutSubView
- 初始化一個(gè)View會(huì)調(diào)用一次View获黔,如果在初始化方法里面改變了frame,也會(huì)再調(diào)用一次在验,后續(xù)修改frame玷氏,除非frame前后值不同,否則便不會(huì)再調(diào)用
- 在一個(gè)方法內(nèi)調(diào)用n次addSubView只會(huì)調(diào)用一次layoutSubView腋舌,一個(gè)方法內(nèi)如果調(diào)用了addSubView就會(huì)調(diào)用layoutSubView
- 每次旋轉(zhuǎn)屏幕都會(huì)調(diào)用layoutSubView一次
- 假如該View繼承于ScrollView盏触,那么該ScrollView在滾動(dòng)的時(shí)候會(huì)不斷調(diào)用layoutSubView
- 顧名思義,修改子View的frame也會(huì)觸發(fā)父View的layoutSubView块饺,但如果只是修改x赞辩、y坐標(biāo),就不會(huì)調(diào)用
UpdateConstraint
UpdateConstraint查閱了資料
主要如下:
- updateConstraints主要是更新View的約束授艰,并會(huì)調(diào)用其所有子視圖的該方法去更新約束
- 當(dāng)純代碼自定義一個(gè)View時(shí)辨嗽,想把約束寫(xiě)在updateConstraint方法中,就一定要重寫(xiě)requiresConstraintBasedLayout方法想诅,返回true