3

What does the multiplier, which is a property of a constraint in Auto Layout, do?

Cesare
  • 9,139
  • 16
  • 78
  • 130

2 Answers2

6

The relationship between two values in a constraint is determined by a formula:

 b = am + c

where a and b are the two values that are to be related, m is the multiplier, and c is the constant.

So for example if one width is to be twice that of another, clearly a multiplier of 2 is going to make sense (and a constant of 0). But if one width is to be 10 more than another, then a constant of 10 is going to make sense (and a multiplier of 1).

The default, obviously, is the multiplier is 1 and the constant is 0. That makes a and b equal.

Extra for experts: Under the hood, part of the reason for the structure of this formula is that you end up with a set of simultaneous linear equations to solve for. This is how the various constraints are resolved to get the actual layout.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thanks once again Matt for your great answers. "But if one width is to be 10 more than another" does this even work between an iPhone 4" view and an iPhone 5.5" view? They got the same size class but they can look different. Thanks! – Cesare Jan 17 '15 at 22:49
  • 1
    It works absolutely everywhere; that's the point. But remember, other things can change one of the values. Constraints are often tied to the ultimate superview, the view controller's main view. But _it_ is a different size on every device type! That is the whole point of auto layout; this view _will_ change size, so we set up constraints so that other things can move / change with it. – matt Jan 17 '15 at 23:06
2

According to Apple's docs: "The constant multiplied with the attribute on the right-hand side of the constraint as part of getting the modified attribute."

It is useful, for instance, if you want one view's height to be 35% of another view's height. In this case, you'd create a constraint making their heights equal with a multiplier of 0.35.

vacawama
  • 150,663
  • 30
  • 266
  • 294