Summary of UIView layout: UIView Layout
Summary: Layout UIView
1. Laying out Subviews (layout Subviews)
The system provides three related APIs.
- (void) layoutSubviews
In IOS5.1 and earlier versions, the default implementation of this method does not do anything (the implementation is empty), and later versions (beginning with IOS6) after IOS5.1, the default implementation of this method is to use the constraints (Autolayout) You set on this view)
Determine the position and size of subviews. If you want to deploy its Subviews more accurately, you can override this method. You can directly set the frame of Subviews in this method.
- (void) setNeedsLayout
This method sets the current layout of the view to invalid and triggers layout update in the next update cycle (update cycle ).
- (void) layoutIfNeeded
Use this method to force layout immediately. Starting from the current view, this method traverses the entire view level (including superviews) to request layout. Therefore, calling this method forces the hierarchical layout of the entire view.
2. Triggering Constraint-Based Layout (triggers Layout-Based)
Some methods in autolayout
- (void)setNeedsUpdateConstraints
When a custom view attribute changes and may affect constratin, you need to call this method to mark that constraints needs to be updated at a certain point in the future, and the system then calls updateConstraints
- (BOOL)needsUpdateConstraints
Constraint-based layout system uses this return value to determine whether to call
updateConstraints
As part of the normal layout process.
- (void)updateConstraints
Custom view should override this method to create constraints in it,
Note: You must call[super updateConstraints]
3. Layout Process
Before the view is displayed, Auto layout introduces two more steps: updating constraints and laying out views.
Each step depends on the previous one. Display depends on layout, while layout depends on updating constraints.
Updating constraints-> layout-> display
Step 1: updating constraints,
It is called the measurement phase. It prepares information for the next layout step from the bottom up (from subview to super view.
You can call setNeedUpdateConstraints to trigger this step.
However, when you customize a view, if some changes may affect the layout, you usually need to notify Auto layout yourself.
When it comes to custom views, You can override the updateConstraints method and add the local contraints required by the view.
Step 2: layout,
From super view to subview, this step mainly applies the information in the previous step to set the center and bounds of the view. You can call setNeedsLayout to trigger this step.
This method does not apply layout immediately. If you want the system to update layout immediately, you can call layoutifNeeded. In addition, you can override layoutSubviews in layout.
Project to get more control.
Step 3: display,
This step renders the view to the screen. It has nothing to do with whether you use Auto layout,
The operation is triggered by calling setNeedsDisplay from top to bottom.
Because each step depends on the previous step, a display may trigger layout. When any layout is not processed, layout may trigger updating constraints, when the constraint system update changes.
These three steps are not one-way. constraint-based layout is an iterative process. During the layout process, constraints may be changed, and updating constraints may be triggered once for a round of layout. This can be used to create an advanced custom view layout,