UIKit automatically loads views from your storyboard file when they are needed. As part of the loading process, UIKit performs the following sequence of tasks:
- Instantiates views using the information in your storyboard file.
- Connects all outlets and actions.
- Assigns the root view to the view controller’s view property.
- Calls the view controller’s awakeFromNib method.
When this method is called, the view controller’s trait collection is empty and views may not be in their final positions. - Calls the view controller’s viewDidLoad method.
Use this method to add or remove views, modify layout constraints, and load data for your views
Before displaying a view controller’s views onscreen, UIKit gives you some additional chances to prepare those views before and after they are onscreen. Specifically, UIKit performs the following sequence of tasks:
- Calls the view controller’s viewWillAppear: method to let it know that its views are about to appear onscreen.
- Updates the layout of the views.
- Displays the views onscreen.
- Calls the viewDidAppear: method when the views are onscreen.
When you add, remove, or modify the size or position of views, remember to add and remove any constraints that apply to those views. Making layout-related changes to your view hierarchy causes UIKit to mark the layout as dirty. During the next update cycle, the layout engine computes the size and position of views using the current layout constraints and applies those changes to the view hierarchy.
Managing View Layout
When the size and position of views changes, UIKit updates the layout information for your view hierarchy. For views configured using Auto Layout, UIKit engages the Auto Layout engine and uses it to update the layout according to the current constraints. UIKit also lets other interested objects, such as the active presentation controller, know abut the layout changes so that they can respond accordingly.
During the layout process, UIKit notifies you at several points so that you can perform additional layout-related tasks. Use these notifications to modify your layout constraints or to make final tweaks to the layout after the layout constraints have been applied. During the layout process, UIKit does the following for each affected view controller:
- Updates the trait collections of the view controller and its views, as needed; see When Do Trait and Size Changes Happen?
- Calls the view controller’s viewWillLayoutSubviews method.
- Calls the containerViewWillLayoutSubviews method of the current UIPresentationController object.
- Calls the layoutSubviews method of view controller’s root view.
The default implementation of this method computes the new layout information using the available constraints. The method then traverses the view hierarchy and calls layoutSubviews for each subview. - Applies the computed layout information to the views.
- Calls the view controller’s viewDidLayoutSubviews method.
- Calls the containerViewDidLayoutSubviews method of the current UIPresentationController object.
View controllers can use the viewWillLayoutSubviews and viewDidLayoutSubviews methods to perform additional updates that might impact the layout process. Before layout, you might add or remove views, update the size or position of views, update constraints, or update other view-related properties. After layout, you might reload table data, update the content of other views, or make final adjustments to the size and position of views.
Here are some tips for managing your layout effectively:
- Use Auto Layout. The constraints you create using Auto Layout are a flexible and easy way to position your content on different screen sizes.
- Take advantage of the top and bottom layout guides. Laying out content to these guides ensures that your content is always visible. The position of the top layout guide factors in the height of the status bar and navigation bar. Similarly, the position of the bottom layout guide factors in the height of a tab bar or toolbar.
- Remember to update constraints when adding or removing views. If you add or remove views dynamically, remember to update the corresponding constraints.
- Remove constraints temporarily while animating your view controller’s views. When animating views using UIKit Core Animation, remove your constraints for the duration of the animations and add them back when the animations finish. Remember to update your constraints if the position or size of your views changed during the animation.
For information about presentation controllers and the role they play in the view controller architecture, see The Presentation and Transition Process.