0%

子视图和viewController创建周期

创建一个UIView

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
-(id)init {
if (self = [super init]) {

UILabel * label = [UILabel new];
label.tag = 100;
[self addSubview:label];

NSLog(@"init");

[self performSelector:@selector(setNeedsUpdateConstraints) withObject:nil afterDelay:5.0f];

}
return self;
}

-(id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
NSLog(@"initWithFrame");
}
return self;
}


- (void)willMoveToSuperview:(nullable UIView *)newSuperview {
NSLog(@"willMoveToSuperview");


}
- (void)didMoveToSuperview {
NSLog(@"didMoveToSuperview%@",NSStringFromCGRect(self.frame));
}
- (void)willMoveToWindow:(nullable UIWindow *)newWindow {
NSLog(@"willMoveToWindow");
}
- (void)didMoveToWindow {
NSLog(@"didMoveToWindow%@",NSStringFromCGRect(self.frame));
}

-(void)updateConstraints {
[super updateConstraints];
NSLog(@"updateConstraints%@",NSStringFromCGRect(self.frame));

UILabel * label = [self viewWithTag:100];
[label makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self);
}];
}

-(void)layoutSubviews {
[super layoutSubviews];
NSLog(@"layoutSubviews%@",NSStringFromCGRect(self.frame));


}

-(void)drawRect:(CGRect)rect {
[super drawRect:rect];
NSLog(@"drawRect%@",NSStringFromCGRect(rect));

}
将此View加入到UIViewController
STPStartButton * btn = [STPStartButton new];
[self.view addSubview:btn];
[btn makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.view);
make.centerY.equalTo(self.view).offset(100.0f);
}];

-(void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
NSLog(@"viewWillLayoutSubviews");
}

-(void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
NSLog(@"viewDidLayoutSubviews");
}

-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(@"viewWillAppear");
}

-(void)updateViewConstraints {
[super updateViewConstraints];
NSLog(@"updateViewConstraints");

}

打印周期日志如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2016-10-08 21:33:32.378 Stuep[12384:5705814] initWithFrame
2016-10-08 21:33:32.379 Stuep[12384:5705814] init
2016-10-08 21:33:32.379 Stuep[12384:5705814] willMoveToSuperview
2016-10-08 21:33:32.379 Stuep[12384:5705814] didMoveToSuperview{{0, 0}, {0, 0}}
2016-10-08 21:33:32.387 Stuep[12384:5705814] viewWillAppear
2016-10-08 21:33:32.387 Stuep[12384:5705814] willMoveToWindow
2016-10-08 21:33:32.388 Stuep[12384:5705814] didMoveToWindow{{0, 0}, {0, 0}}
2016-10-08 21:33:32.401 Stuep[12384:5705814] updateConstraints{{0, 0}, {0, 0}}
2016-10-08 21:33:32.404 Stuep[12384:5705814] updateViewConstraints
2016-10-08 21:33:32.405 Stuep[12384:5705814] viewWillLayoutSubviews
2016-10-08 21:33:32.406 Stuep[12384:5705814] viewDidLayoutSubviews
2016-10-08 21:33:32.407 Stuep[12384:5705814] layoutSubviews{{172.5, 416.5}, {30, 34}}
2016-10-08 21:33:32.408 Stuep[12384:5705814] viewWillLayoutSubviews
2016-10-08 21:33:32.410 Stuep[12384:5705814] viewDidLayoutSubviews
2016-10-08 21:33:32.410 Stuep[12384:5705814] drawRect{{0, 0}, {30, 34}}
2016-10-08 21:33:37.380 Stuep[12384:5705814] viewWillLayoutSubviews
2016-10-08 21:33:37.381 Stuep[12384:5705814] updateConstraints{{172.5, 416.5}, {30, 34}}
2016-10-08 21:33:37.382 Stuep[12384:5705814] viewDidLayoutSubviews

最后三行是由于在创建成功后5秒执行了子View的setNeedsUpdateConstraints方法.