[英]How can I get real frame from Autolayout
I create a view(width:600) with xib and put a UIScrollView(margin both left and right 20) in it. 我用xib创建了一个view(width:600),并在其中放了一个UIScrollView(左边和右边分别为20)。 I want to get the scrollView's width in code but I get 560 instead. 我想在代码中获取scrollView的宽度,但我却得到560。 I'm new to AutoLayout and really want some help, thanks a lot. 我是AutoLayout的新手,真的很需要帮助,非常感谢。
It's all a question of when you get the scroll view's width in code. 这是何时获取代码中滚动视图宽度的问题。 viewDidLoad
is too soon. viewDidLoad
为时过早。 viewDidAppear
would be okay. viewDidAppear
可以。 But the best time is viewDidLayoutSubviews
, because that's when layout has been performed and the scroll view's width has been set. 但是最好的时间是viewDidLayoutSubviews
,因为那是执行布局并设置滚动视图的宽度的时间。
You have to load your XIB and call layoutIfNeeded
. 您必须加载XIB并调用layoutIfNeeded
。 At the next runloop cycle you will be able to see your actual frame size. 在下一个运行循环周期中,您将能够看到实际的帧大小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.