I create a view(width:600) with xib and put a UIScrollView(margin both left and right 20) in it. I want to get the scrollView's width in code but I get 560 instead. I'm new to AutoLayout and really want some help, thanks a lot.
It's all a question of when you get the scroll view's width in code. viewDidLoad
is too soon. viewDidAppear
would be okay. But the best time is viewDidLayoutSubviews
, because that's when layout has been performed and the scroll view's width has been set.
You have to load your XIB and call layoutIfNeeded
. At the next runloop cycle you will be able to see your actual frame size.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.