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

说下什么情况下会触发布局的自动更新,怎么手动更新布局 #131

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

Comments

@tbfungeek
Copy link
Owner

tbfungeek commented Feb 19, 2020

和约束一样,布局分为自动更新布局和手动更新布局,自动更新布局会在如下场景下发生:

1、init初始化不会触发layoutSubviews,但是用initWithFrame进行初始化的时候,当rect的值不是CGRectZero时也会触发
2、addSubview会触发layoutSubviews。
3、设置view的frame会触发layoutSubviews,当然前提是frame的值设置前后发生了变化。
4、改变一个UIView大小的时候也会触发父UIView上的layoutSubviews事件。
5、滚动一个UIScrollView会触发layoutSubviews。
6、旋转屏幕会触发父UIView上的layoutSubviews事件。

如果我们需要手动更新布局可以调用:

- (void)setNeedsLayout;

这样会在RunLoop进入休眠之前更新布局,如果想要立刻生效,可以调用:

- (void)layoutIfNeeded;

当更新布局的时候

- (void)layoutSubviews; 

方法将会被触发。

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