AutoLayout

As a graph cutter, I have never studied AutoLayout systematically, and in the recent work of many graphs cutter, PROBLEMS related to AutoLayout often get stuck.

During the interview, I was often asked about some concepts related to AutoLayout, but the answer was not very good.

Just one requirement finished, to recharge the unfamiliar concept of AutoLayout.

multiplier

From: StackOverflow

  • What if you need to implement two Views with 1:1 width?

Easy. Add an EqualWidths constraint

  • What if I need to implement two views 2:3 wide?

This is where multipiler is needed

Set the multiplier to 2:3, as shown

  • I’ll retrieve the next thing.

Just do a little digging and you’ll find out

make.left.equalTo(view.mas_right).multipliedBy(4.0/3.0)
Copy the code

priority

What about priority, which is also common? Google: What is Constraint Priority and how to use it

Key notes:

  • This is the sixth (also last) part of Making Sense of Auto Layout Series.

That’s it for AutoLayout, don’t be afraid

  • Constant priority is a number to determine how important is that constraint. The number can range from 1 to 1000, the higher the number goes, the more important a constraint is.

Priority is the importance of constraints, from 1 to 1000

  • What does important mean? When there are conflicting constraints, Auto Layout Engine will attempt to break / ignore constraint with lower priority number first, hence making constraint with higher priority having higher importance.

Importance means that the AutoLayout engine ignores or breaks low-priority constraints when there are conflicting constraints

  • If there are two conflicting constraints with the same priority number (making it a tie), Xcode will show you red lines with numbers.

When two constraints are not only contradictory, but have equal priority, Xcode shows the red line

Oh, that’s it. That’s it

  • When you define a new constraint in Interface Builder / Storyboard, Xcode will auto assign the constraint priority as 1000. 1000 is the highest constraint priority, meaning it is mandatory as no other constraint can have higher priority than this and this constraint will never be ignored in favor of others.

When you create a new constraint, Xcode gives it a default priority of 1000. 1000 means mandatory, highest priority and cannot be ignored

  • On the other hand, constraints with priority less than 1000 (1-999) are called as optional constraint as it might be ignored in favor of other constraint which have higher constraint priority.

Priorities below 1000 are optional constraints that may be ignored by higher-priority constraints

  • When you normally create a constraint (Xcode default set it to 1000 priority, mandatory constraint), it will show as a blue solid line like this :
  • If you change the constraint priority to any number lower than 1000 (optional constraint), it will show dashed line like this :

Oh, shit. How am I supposed to know

  • Constraint priority is useful when you have possible conflicting constraint and you want to tell Xcode which of them can be safely ignored when conflict arise in order to solve conflict.

Priority is useful when there are potentially conflicting constraints. You can use this to tell Xcode which constraints can be safely ignored to resolve conflicts

  • Usually you will want to modify constraint priority of a constraint which uses larger or equal to (>=) / smaller or equal to (<=) instead of plain “equal” (==).

Usually for constraints that use “>=” or “<=”, you might adjust its priority