Reputation: 100
I have a UIViewController for which I'm using Autolayout via IB to position and size its subviews.
For some reason I was not getting the correct UIView layout and I decided to log all the subview sizes in the VC.
I logged subview sizes in:
viewDidLoad{
self.view.layoutIfNeeded()
print(uiview.size)
}
viewDidLayoutSubviews{
print(uiview.size)
}
viewWillAppear{
print(uiview.size)
}
viewDidAppear{
print(uiview.size)
}
the subview sizes are correct in :
but it becomes incorrect in:
How can I use correct subview sizes from the ViewWillAppear? And why are my sizes incorrect in the final viewDidLayoutSubviews call and in viewDidAppear?
Upvotes: 1
Views: 5406
Reputation: 100
I solved my own question.
I constrain views proportional to VC's height.
The VC that I'm displaying has a Navigation Bar. All the view sizes are correct,(even in viewDidLoad because I call layoutIfNeeded()), until the VC starts drawing the Navigation Bar and shrinks the Superview's height; thus, shrinking its subview's heights.
All I have to do is to Extend edges under the top bar by ticking the appropriate box in the VC's Attributes Inspector. This way VC will not shrink the superview of my subviews.
Upvotes: 2
Reputation: 4270
Your "correct sizes" are from before AutoLayout has done its work. The frames will be the frames from the Interface Builder document. You need to fix your constraints to achieve whatever frames you are expecting.
AutoLayout hasn't completed until after the final viewDidLayoutSubviews call just before viewDidAppear.
Upvotes: 5