None of the above answers worked completely for me, except viewDidLoad
but that has the side effect of not displaying what I want until after the view has animated in, which looks poor.
viewDidLayoutSubviews
should be the correct place to run code that relies on the autolayout being complete, but as others have pointed out it is called multiple times in recent iOS versions and you can't know which is the final call.
So I resolved this with a small hack. In my storyboard, mySubview
should be smaller than its containing self.view
. But when viewDidLayoutSubviews
is first called, mySubview
still has a width of 600, whereas self.view
seems to be set correctly (this is an iPhone project). So all I have to do is monitor subsequent calls and check the relative widths. Once mySubview
is smaller than self.view
I can be sure it has been laid out correctly.
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if self.mySubview.bounds.size.width < self.view.bounds.size.width {
// mySubview's width became less than the view's width, so it is
// safe to assume it is now laid out correctly...
}
}
This has the advantage of not relying on hard-coded numbers, so it can work on all iPhone form factors, for example. Of course it may not be a panacea in all cases or on all devices, but there are probably many ingenious ways to do similar checks of relative sizes.
And no, we shouldn't have to do this, but until Apple gives us some more reliable callbacks, we're all stuck with it.
viewDidLoad
. Which method gets invoked after the auto layout has been completed. The reason I ask is because I want to calculate the width before the user can interact with the textView. So logging the object description might not help bcuz I want to use the width for some other calculation. – Gentoo