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坐标即可
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容