Autolayer Automatic layout process and related methods in iOS

Source: Internet
Author: User

About UIView's Layer,ios offers three methods:

1,layoutsubviews

In iOS5.1 and previous versions, the default implementation of this method will not do anything (implementation is empty), after iOS5.1 (iOS6 start) version, the default implementation of this method is to use the constraints (AutoLayout) You set above this view To decide Subviews's position and size. Subclasses of UIView You can override this method if you need to make a more precise layout of its subviews. autoresizing constraint-based behaviors of subviews We should override this method only if and when we cannot provide the layout results we want. You can set the Subviews frame directly in this method. We should not call this method directly, but should use the following two methods.

2, Setneedslayout

This method sets the view's current layout to invalid and triggers the layout update in the next Upadte cycle.

3, layoutifneeded

Use this method to force layout immediately, starting with the current view, which traverses the entire view hierarchy (including superviews) request layout. Therefore, calling this method forces the entire view hierarchy layout.

Method of constraint-based autolayer:

1, Setneedsupdateconstraints

When a property of a custom view changes and may affect constraint, this method needs to be called to mark constraints to be updated at some point in the future, and then called by the system updateConstraints .

2, Needsupdateconstraints

The constraint-based layout system uses this return value to determine whether a call needs to be called updateConstraints as part of the normal layout process.

3, updateconstraintsifneeded

Immediately triggers a constraint update to automatically update the layout.

4, Updateconstraints

Custom view should override this method in which the constraints is established. Note: To be called at the end of the implementation[super updateConstraints]

Auto Layout process

Compared to using springs and struts (autoresizingmask), Auto layout introduces two more steps before the view display: updating constraints and laying out views. Each step is dependent on the previous one. Display relies on layout, while layout relies on updating constraints.updating constraints->layout->display

The first step: updating constraints, known as the measurement phase, from the bottom up (from Subview to Super View), prepares the information for the next layout. This step can be setNeedUpdateConstraints triggered by invoking the method. The change of constraints will also automatically trigger this step. However, when you customize the view, if some changes may affect the layout, you usually need to notify auto layout,updateconstraintsifneeded yourself.

For custom view, you can usually override the Updateconstraints method, where you can add the local contraints that the view requires.

Step two: Layout, which is from the top down (from the Super View to Subview), this step mainly applies the previous step of the information to set the view center and bounds. This step can be triggered by calling Setneedslayout, which does not immediately apply layout. If you want the system to update layout immediately, you can call layoutifneeded. In addition, custom view can override method Layoutsubviews to get more customized results in the layout project.

The third step: display, this step when the view is rendered to the screen, it is not related to whether you use auto layout, the operation is from the top down (from Super view to Subview), by calling Setneedsdisplay Trigger,

Because each step relies on the previous step, a display may trigger layout, and when any layout is not processed, layout may trigger updating constraints when constraint When the system update changes.

Note that these three steps are not one-way, constraint-based layout is an iterative process, the layout process, may go to change the constraints, there is a trigger updating constraints, A round of layout process.

Note: If you invoke a custom layoutsubviews every time it will cause another layout to pass, you will be stuck in an infinite loop.

Such as:

Autolayer Automatic layout process and related methods in iOS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.