美文网首页iOS归纳
iOS Masonry布局不能立即获取frame解决方案

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

作者: 久林的技术随笔 | 来源:发表于2019-08-27 20:03 被阅读0次

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解决方案

    本文链接:https://www.haomeiwen.com/subject/xueyectx.html