AutoLayout Constraint Precedence

Source: Internet
Author: User
Tags dashed line

Code calculation--autoreszing (relationship between parent and child controls), AutoLayout (any control can produce a relationship), Sizeclass

1, Brief introduction
Each constraint has a priority in AutoLayout, with a priority range of 1 to 1000, and the default created constraint priority is the highest of 1000.

At the core of my understanding of constraint precedence is the issue of resolving constraint conflicts If there are multiple sets of constraints . Some scenarios need to be laid out dynamically, such as we have three buttons on the vertical:

If the requirement is that the second block sometimes exists during the run, sometimes it does not exist, and if the second block does not exist, the first block will go to the position of the second block, and then how to set it up.

2, Solution
One option is to let him turn on the second set of constraints after the disappearance, but if the same control, such as the first block from the second 17, while the distance from the third is also 17, that is, to maintain the vertical distance and disappear after the second existence of the same distance from the bottom block, this time the constraint will conflict, Since the constraint satisfies two conditions at the bottom of the control, it is not possible to satisfy it at the same time, as soon as you let the person meet the height of 1.8-meter and the height of 1.61-meter, then we can use a lower constraint priority, so that another constraint takes precedence on the line.

Layout:
First we press the normal layout first.

Click to delete the second button, you will find the following phenomenon



The first button disappears at the same time? Later, the constraint is checked, the first button is found equal width, and so on, leading constraints are associated with the second button, then the second view is deleted, the first view must have no corresponding effective constraints, the problem is inevitable. Therefore, to achieve the initial goal, the first view constraint must be set to the third related, that is, the third view, such as width, leading and the third is the same. But the only one that is currently reserved is the vertical spacing of the first view and the second view, which cannot be removed. Because the VIEW2 exists, it is necessary to have a spacing of the same view1, only when there is no time to consider the setting of the vertical spacing constraint of view1 with VIEW3. Then the vertical spacing of view1 can not be said to be set at the same time two. What do we do? Then there is a priority, the vertical spacing of view1 to VIEW3 is also set to 17, but the priority is set to 999, so that when VIEW2 exists, the system will first match View1 to view3 vertical spacing according to priority, if VIEW2 is deleted, Before the spacing is set to be effective, it will match the lower priority of the second view1 to VIEW3 vertical distance constraints, you can achieve the goal.

The image above sets the vertical distance from View1 to VIEW3

Set the equal width, equal height, leadig property of View1 with VIEW3


A vertical constraint of view1 to View2 is also set


Lower the priority of the vertical distance from View1 to VIEW3



A constraint that lowers precedence turns into a dashed line.

The

runs again, achieving the final effect. When the view2 is displayed view2,view2 deleted, View1 automatically moves to view2 original position.

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.