將相關代碼添加打印献丑。
- (instancetype)init{
if (self = [super init]) {
NSLog(@"%s",__func__);
}
return self;
}
//通過代碼創(chuàng)建控件就會調用這個方法
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
NSLog(@"%s",__func__);
}
return self;
}
//通過storyboared或者xib中創(chuàng)建控件就會調用這個方法
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
NSLog(@"%s",__func__);
}
return self;
}
- (void)awakeFromNib {
[super awakeFromNib];
NSLog(@"%s",__func__);
}
//如果在initWithFrame中添加子視圖會調用兩次
- (void)layoutSubviews{
[super layoutSubviews];
NSLog(@"%s",__func__);
}
- (void)didAddSubview:(UIView *)subview{
[super didAddSubview:subview];
NSLog(@"%s",__func__);
}
- (void)willRemoveSubview:(UIView *)subview{
[super willRemoveSubview:subview];
NSLog(@"%s",__func__);
}
- (void)willMoveToSuperview:(nullable UIView *)newSuperview{
[super willMoveToSuperview:newSuperview];
NSLog(@"%s",__func__);
}
- (void)didMoveToSuperview{
[super didMoveToSuperview];
NSLog(@"%s",__func__);
}
- (void)willMoveToWindow:(nullable UIWindow *)newWindow{
[super willMoveToWindow:newWindow];
NSLog(@"%s",__func__);
}
- (void)didMoveToWindow{
[super didMoveToWindow];
NSLog(@"%s",__func__);
}
- (void)removeFromSuperview{
[super removeFromSuperview];
NSLog(@"%s",__func__);
}
- (void)dealloc{
NSLog(@"%s",__func__);
}
當創(chuàng)建view時
2017-11-06 10:35:12.347153+0800 IOSLife[7587:2353869] -[MyView willMoveToSuperview:]
2017-11-06 10:35:12.347312+0800 IOSLife[7587:2353869] -[MyView didMoveToSuperview]
2017-11-06 10:35:12.353483+0800 IOSLife[7587:2353869] -[MyView willMoveToWindow:]
2017-11-06 10:35:12.353644+0800 IOSLife[7587:2353869] -[MyView didMoveToWindow]
2017-11-06 10:35:12.363861+0800 IOSLife[7587:2353869] -[MyView layoutSubviews]
2017-11-06 10:35:12.655946+0800 IOSLife[7607:2356750] -[MyView drawRect:]
當view銷毀時
2017-11-06 10:41:28.152448+0800 IOSLife[7607:2356750] -[MyView willMoveToWindow:]
2017-11-06 10:41:28.152693+0800 IOSLife[7607:2356750] -[MyView didMoveToWindow]
2017-11-06 10:41:28.155160+0800 IOSLife[7607:2356750] -[MyView willMoveToSuperview:]
2017-11-06 10:41:28.155281+0800 IOSLife[7607:2356750] -[MyView didMoveToSuperview]
2017-11-06 10:41:28.155336+0800 IOSLife[7607:2356750] -[MyView removeFromSuperview]
2017-11-06 10:41:28.155399+0800 IOSLife[7607:2356750] -[MyView dealloc]
可以看出上面方法中只會執(zhí)行一次的方法有removeFromSuperview
诫硕、dealloc
兩個方法(layoutSubviews在子視圖布局變動時會多次調用)溺职,所以可以在這兩個方法中執(zhí)行釋放內存等操作(e.g:移除觀察者姜贡,定時器等)冯吓。
給view添加子視圖時
2017-11-06 10:45:47.749310+0800 IOSLife[7614:2358053] -[MyView didAddSubview:]
2017-11-06 10:45:47.750111+0800 IOSLife[7614:2358053] -[MyView layoutSubviews]
didAddSubview:
和willRemoveSubview:
需要有子視圖才能執(zhí)行儡率。
此時再銷毀該view
2017-11-06 10:46:28.022473+0800 IOSLife[7617:2358497] -[MyView willMoveToSuperview:]
2017-11-06 10:46:28.022643+0800 IOSLife[7617:2358497] -[MyView didMoveToSuperview]
2017-11-06 10:46:28.031533+0800 IOSLife[7617:2358497] -[MyView willMoveToWindow:]
2017-11-06 10:46:28.031738+0800 IOSLife[7617:2358497] -[MyView didMoveToWindow]
2017-11-06 10:46:28.036048+0800 IOSLife[7617:2358497] -[MyView layoutSubviews]
2017-11-06 10:46:28.719103+0800 IOSLife[7617:2358497] -[MyView didAddSubview:]
2017-11-06 10:46:28.719873+0800 IOSLife[7617:2358497] -[MyView layoutSubviews]
2017-11-06 10:46:30.529769+0800 IOSLife[7617:2358497] -[MyView willMoveToWindow:]
2017-11-06 10:46:30.530059+0800 IOSLife[7617:2358497] -[MyView didMoveToWindow]
2017-11-06 10:46:30.532931+0800 IOSLife[7617:2358497] -[MyView willMoveToSuperview:]
2017-11-06 10:46:30.533055+0800 IOSLife[7617:2358497] -[MyView didMoveToSuperview]
2017-11-06 10:46:30.533112+0800 IOSLife[7617:2358497] -[MyView removeFromSuperview]
2017-11-06 10:46:30.533183+0800 IOSLife[7617:2358497] -[MyView dealloc]
2017-11-06 10:46:30.533256+0800 IOSLife[7617:2358497] -[MyView willRemoveSubview:]
willRemoveSubview
是在dealloc
后面執(zhí)行的。如果有多個子視圖胖喳,willRemoveSubview會
循環(huán)執(zhí)行泡躯,直到移除所有子視圖。
注意:
- (void)willMoveToSuperview:(nullable UIView *)newSuperview;
和- (void)willMoveToWindow:(nullable UIWindow *)newWindow;
這倆個方法可以根據(jù)參數(shù)判斷丽焊,nil則為銷毀较剃,否則為創(chuàng)建;- (void)didMoveToSuperview;
和- (void)didMoveToWindow;
可以根據(jù)self.superview判斷技健,nil則為銷毀写穴,否則為創(chuàng)建。- 如果在
initWithFrame
中有view的初始化相關操作雌贱,layoutSubviews
會調用兩次确垫。layoutSubviews
只進行布局相關處理,不能有view初始化操作(當子視圖布局改變時會調用帽芽,能多次調用)删掀,下面是layoutSubviews
的觸發(fā)條件:
1、init
初始化不會觸發(fā)layoutSubviews
,但是是用initWithFrame
進行初始化時导街,當rect的值不為CGRectZero時,也會觸發(fā)披泪。
2、addSubview
會觸發(fā)layoutSubviews
搬瑰。
3款票、設置view的Frame會觸發(fā)layoutSubviews
控硼,當然前提是frame的值設置前后發(fā)生了變化。
4艾少、滾動一個UIScrollView會觸發(fā)layoutSubviews
卡乾。
5、旋轉Screen會觸發(fā)父UIView上的layoutSubviews
缚够。
6幔妨、改變一個UIView大小的時候也會觸發(fā)父UIView上的layoutSubviews
。