willMoveToSuperview - didMoveToSuperview - didAddSubview - willMoveToWindow - didMoveToWindow - layoutSubviews
话不多少,直接看demo
#import "TestView.h"
@interface TestView (){
NSInteger count;
}
@end
@implementation TestView
- (instancetype)init{
self = [super init];
if (self) {
count = 0;
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
count++;
NSLog(@"%@ ==> %ld",NSStringFromSelector(_cmd),count);
}
- (void)didAddSubview:(UIView *)subview{
[super didAddSubview:subview];
count++;
NSLog(@"%@ ==> %ld",NSStringFromSelector(_cmd),count);
}
- (void)willRemoveSubview:(UIView *)subview{
[super willRemoveSubview:subview];
count++;
NSLog(@"%@ ==> %ld",NSStringFromSelector(_cmd),count);
}
- (void)willMoveToSuperview:(nullable UIView *)newSuperview{
[super willMoveToSuperview:newSuperview];
count++;
NSLog(@"%@ ==> %ld",NSStringFromSelector(_cmd),count);
}
- (void)didMoveToSuperview{
[super didMoveToSuperview];
count++;
NSLog(@"%@ ==> %ld",NSStringFromSelector(_cmd),count);
}
- (void)willMoveToWindow:(nullable UIWindow *)newWindow{
[super willMoveToWindow:newWindow];
count++;
NSLog(@"%@ ==> %ld",NSStringFromSelector(_cmd),count);
}
- (void)didMoveToWindow{
[super didMoveToWindow];
count++;
NSLog(@"%@ ==> %ld",NSStringFromSelector(_cmd),count);
}
- (void)removeFromSuperview{
[super removeFromSuperview];
count++;
NSLog(@"%@ ==> %ld",NSStringFromSelector(_cmd),count);
}
- (void)dealloc{
count++;
NSLog(@"%@ ==> %ld",NSStringFromSelector(_cmd),count);
}
打印结果:
2019-03-08 22:43:00.222184+0800 生命周期[3259:88167] willMoveToSuperview: ==> 1
2019-03-08 22:43:00.222329+0800 生命周期[3259:88167] didMoveToSuperview ==> 2
2019-03-08 22:43:00.222510+0800 生命周期[3259:88167] didAddSubview: ==> 3
2019-03-08 22:43:00.229727+0800 生命周期[3259:88167] willMoveToWindow: ==> 4
2019-03-08 22:43:00.229869+0800 生命周期[3259:88167] didMoveToWindow ==> 5
2019-03-08 22:43:00.260830+0800 生命周期[3259:88167] layoutSubviews ==> 6
layout机制相关方法
- (CGSize)sizeThatFits:(CGSize)size
- (void)sizeToFit
- (void)layoutSubviews
- (void)layoutIfNeeded
- (void)setNeedsLayout
- (void)setNeedsDisplay
- (void)drawRect
layoutSubviews 调用时机:
1、init初始化不会触发layoutSubviews
但是是用initWithFrame 进行初始化时,当rect的值不为CGRectZero时,也会触发
2、addSubview会触发layoutSubviews
如果添加的子控件没有Frame,不会调用;
3、设置view的Frame会触发layoutSubviews,当然前提是frame的值设置前后发生了变化
还有一个前提是该View 已经被添加到父控件, 此时View和其父控件的layoutSubviews都会调用;
4、滚动一个UIScrollView会触发layoutSubviews
5、旋转Screen会触发父UIView上的layoutSubviews事件
6、改变一个UIView大小的时候也会触发父UIView上的layoutSubviews事件
在苹果的官方文档中强调:
You should override this method only if the autoresizing behaviors of the subviews do not offer the behavior you want.
layoutSubviews, 当我们在某个类的内部调整子视图位置时,需要调用。
反过来的意思就是说:如果你想要在外部设置subviews的位置,就不要重写。
刷新子对象布局
-layoutSubviews
:这个方法,默认没有做任何事情,需要子类进行重写
setNeedsLayout: 标记为需要重新布局,异步调用layoutIfNeeded
刷新布局,不立即刷新
,但layoutSubviews
一定会被调用
layoutIfNeeded:如果,有需要刷新的标记,立即调用layoutSubviews
进行布局(如果没有标记,不会调用layoutSubviews)
重绘
-drawRect:(CGRect)rect
方法:重写此方法,执行重绘任务
-setNeedsDisplay
方法:标记为需要重绘,异步调用drawRect
-setNeedsDisplayInRect:(CGRect)invalidRect
方法:标记为需要局部重绘
sizeToFit
会自动调用sizeThatFits
方法;
sizeToFit
不应该在子类中被重写,应该重写sizeThatFits
sizeThatFits
传入的参数是receiver当前的size,返回一个适合的size
sizeToFit
可以被手动直接调用
sizeToFit
和sizeThatFits
方法都没有递归,对subviews
也不负责,只负责自己
layoutSubviews
对subviews
重新布局
layoutSubviews
方法调用先于drawRect
setNeedsLayout
在receiver
标上一个需要被重新布局的标记,在系统runloop的下一个周期自动调用layoutSubviews
layoutIfNeeded
方法如其名,UIKit会判断该receiver是否需要layout.根据Apple官方文档,layoutIfNeeded
方法应该是这样的
layoutIfNeeded
遍历的不是superview链,应该是subviews链
drawRect是对receiver的重绘,能获得context
setNeedDisplay
在receiver标上一个需要被重新绘图的标记,在下一个draw周期自动重绘,iphone device的刷新频率是60hz,也就是1/60秒后重绘。
参考文章:https://blog.csdn.net/qq_31810357/article/details/79438212