Auto Layout = ViewWillAppear has the wrong width of the view

autolayout, ios

Solution

Layout happens after `viewWillAppear` returns. Do it in `viewDidLayoutSubviews` (in your view controller) or in `layoutSubviews` (in your custom view subclass). Either way, be sure to call `super` first.

Another way is to force layout by sending `[view layoutIfNeeded]`. Then you can add subviews and set their springs and struts. The system will perform layout again (at its usual time) because you added subviews.

Problem

I am using autolayout for parts of my UI. When I check the width of my view in viewWillAppear, the width has not yet updated. It gets updated after the second callback to - (void)updateViewConstraints. The issue is I set up my parts of my UI (using spring and struts for legacy reasons) based on this width in viewWillAppear. When is the good time to set up this legacy UI which takes in a width parameter ? thanks

Original source