Autolayout is not updating frames instantly in iOS

2019-05-31 01:50发布

I'm using autolayout in XCode6.

I have applied the following constraints to a scrollview.

enter image description here

I have tried to access the frame of this scrollview in -viewDidLoad and -viewWillAppear methods for iPhone 4Inchs device. Here problem is, the scrollview's width is showing 600 in Log. But the width should be 300 in 4Ichs device as i'm using autolayout. Seems autolayout will apply after some time interval. It is showing correct width in -viewDidAppear method as 300.

Is there any way to access the original frames in -viewDidLoad and -viewWillAppear methods ?

Thanks in advance.

3条回答
Bombasti
2楼-- · 2019-05-31 02:21

Auto layout doesn't give you the "right" frame sizes until a layout pass has happened. viewDidLoad is too early, and off the top of my head willAppear might be too. viewDidLayoutSubviews is the right place.

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-05-31 02:27

Place this code in your viewDidLoad()

self.view.setNeedsLayout();
self.view.layoutIfNeeded();

Before accessing scorllView.frame.size.width

查看更多
闹够了就滚
4楼-- · 2019-05-31 02:33

From the viewDidLoad and viewWillAppear its shows same as interface builder's dimensions. if you apps relay on scrollView frame(if you want them before viewDidAppear) call viewController instance view method before it presenting.

UIViewController *myVC = [[UIViewController alloc]init];
[myVC view]
[self.navigationController pushViewController:myVC];

But this counting on apps performance.

查看更多
登录 后发表回答