Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

介绍下UIView的生命周期 #126

Open
tbfungeek opened this issue Feb 19, 2020 · 0 comments
Open

介绍下UIView的生命周期 #126

tbfungeek opened this issue Feb 19, 2020 · 0 comments

Comments

@tbfungeek
Copy link
Owner

tbfungeek commented Feb 19, 2020

关于UIView 的生命周期打算从它的一些接口进行解释:

一般在父View 所处的ViewController viewDidLoad之后,我们会调用addSubview将当前View添加到父View,在这个过程中会回调:

- (void)willMoveToSuperview:(UIView *)newSuperview;
- (void)didMoveToSuperview;

紧接着在父View 所处的ViewController 执行viewWillApear之后,当前View会被添加到Window上,这时候会调用:

- (void)willMoveToWindow:(UIWindow *)newWindow;
- (void)didMoveToWindow;

告诉当前View window发生了变化。

紧接着父View 所处的ViewController会进入布局阶段,父View 所处的ViewController在调用完viewWillLayoutSubviewsviewDidLayoutSubviews之后,当前view的

- (void)layoutSubviews;  

将会被调用,在这里我们需要对当前View的子View进行布局。再这之后如果有绘制需求,则会调用:

- (void)drawRect:(CGRect)rect;

当父View 所处的ViewController的所有子View都完成这些阶段后才会调用viewDidApear.

UIView生命周期移除阶段。会先调用willMoveToWindow表示当前view将会从window上移除,继而调用didMoveToWindow,这里需要注意的是这时候传入的window为nil,然后再removeFromSuperView,dealloc,dealloc之后再removeSubview。

下面是关于约束,布局,绘制,相关的方法:

- (void)setNeedsUpdateConstraints
- (void)updateConstraintsIfNeeded;
- (void)updateConstraints;
- (BOOL)needsUpdateConstraints;
- (void)setNeedsLayout;
- (void)layoutIfNeeded;
- (void)layoutSubviews;  
- (void)setNeedsDisplay;
- (void)drawRect:(CGRect)rect;
- (void)setNeedsDisplayInRect:(CGRect)rect;

下面是操作当前View 子View相关的方法:

- (void)addSubview:(UIView *)view;
- (void)didAddSubview:(UIView *)subview;
- (void)willRemoveSubview:(UIView *)subview;
- (void)removeFromSuperview;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant