Search code examples
iosobjective-cuiviewconstraintsviewdidload

Is there a way to get a constraints-applied UIView frame in ViewDidLoad()?


I need to know the exact position of an UIView frame created by a storyboard.

The problem is I get a non-constraints-applied frame in ViewDidLoad().

This drives me crazy all the time.

Because of this problem, I oftentimes abandon the whole idea of designing with storyboard.

I'd like to know if there's an elegant way to solve this initialization issue.

I've already tried awakeFromNib().


Solution

  • In viewDidLoad is called the constraints are not applied on the view.The constraints are applied on the viewDidLayoutSubviews. But With my work I found that viewDidLayoutSubviews worked fine in the ios8 but not always for iOS7 backwards. So my suggestion is that if your app is ios8 onwards use viewDidLayoutSubviews and if not what I'd suggest to hide the view in viewDidLoad and unhide it in viewWillAppear, only after the constraints have been applied. It gets you a small extra delay when loading the screen but the constraints change gets transparent to the user.It is very good from user point of view.