iOS Masonry布局不能立即获取frame解决方案

iOS Masonry布局不能立即获取frame解决方案

1.原因:

摘自:stackflow
https://stackoverflow.com/questions/46387298/why-i-cant-get-the-frame-immediately-when-i-use-masonry
Masonry is a wrapper for autolayouts, and autolayouts calculate itself frame in
-(void)layoutSubviews; method, and only after that u can get frames of all views.
Masonry自动布局,其autolayouts在 layoutSubviews方法中计算自身frame,只有在layoutSubviews才能获得所有视图的frame。

2.解决方案:

在使用Masonry设置完约束后,立刻刷新布局:

[self setNeedsLayout];//标记view需要刷新布局
[self layoutIfNeeded];// 中断当前runloop,并刷新布局
// 在这两个方法之后再获取frame坐标即可

你可能感兴趣的:(iOS Masonry布局不能立即获取frame解决方案)