I am learning how Auto layout works in iOS. For this I created a sample single view application which has one view controller (which is initial view controller) and one custom View (lets call it a sample view). I added sample view to the view controller in the Main.storyboard with auto-layouting constraints. I added a button and a label to Sample View with auto-layouting constraints. I override layoutSubviews and updateConstraints method in SampleView.m. These overridden methods do nothing but calling their super implementation. I added a break point to both of these methods and start debugging the application. Following are my observation
The layout process generally works like this:
First step: updating constraints, happens bottom-up. Triggered by calling [view setNeedsUpdateConstraints], override [view updateConstraints] for custom views. This step solves constraints.
Second step: Layout, happens top-down. Triggered by calling [view setNeedsLayout], [view layoutIfNeeded], override [view layoutSubviews] for custom views. When layoutSubviews get called, we have frames.
Third step: display, happens top-down. Triggered by calling [view setNeedsDisplay], override [view drawRect:] for custom views.
updateConstraints happens before layoutSubviews. Constraints are basically objects added into a view's parent view, describing how to layout the parent view's subviews. So you do need to update constraints before layout happens.
On orientation changes, constraints hasn't changed, therefore I think updateConstraints will not be called.
How can we invoke the updateConstraint method on all views in the view hierarchy when the orientation changes? Constraints don't need to get updated because they are the way about how views should be related to each other. Instead, when orientation changes, the top most view's (UIWindow) frame will be updated, therefore all of its subviews need to update their frame in order to conform to the constraints you has previously set up.
If I am wrong please correct me :)