ios 子视图和viewController创建周期

创建一个UIView

-(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");

}

打印周期日志如下

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方法.