1

An autolayout constraint can be definded as:

self.view.addConstraint(NSLayoutConstraint(item: label, 
       attribute: .Bottom, 
       relatedBy: .Equal, 
       toItem: self.view, 
       attribute: .Bottom, 
       multiplier: 1, 
       constant: 0))

I did not understand what a use case would be for using the multiplier.

When to use Multiplier in iOS AutoLayout?

Michael
  • 32,527
  • 49
  • 210
  • 370

3 Answers3

6

One use case that I commonly use is when I want one view to be 30% of the width of another view. It would look like this:

self.view.addConstraint(NSLayoutConstraint(item: label, 
   attribute: .Width, 
   relatedBy: .Equal, 
   toItem: self.view, 
   attribute: .Width, 
   multiplier: 0.3, 
   constant: 0))
vacawama
  • 150,663
  • 30
  • 266
  • 294
4

If you have two view. and your requirement is that one view is always half of other view or may be 10% of other view than you can use multiplier.

for ex,

enter image description here

i want this two view always in this proposition.

enter image description here

so drag and drop from one view to another and select equal height.

bt it shows errors that both view not in same height.

so select heightenter image description here

now you have to change multiplier by calculating like (second_view_height/first_view_height). and add that in multiplier

enter image description here

this way you use in storyboard

for manual coding you can use this way

self.view.addConstraint(NSLayoutConstraint(item: label, 
   attribute: .Width, 
   relatedBy: .Equal, 
   toItem: self.view, 
   attribute: .Width, 
   multiplier: 0.4869, 
   constant: 0))
Pooja Patel
  • 626
  • 8
  • 12
0

In Apple documentation :

Auto Layout calculates the first item’s attribute to be the product of the second item’s attribute and this multiplier. Any value other than 1 creates a proportional constraint.

Take a look at this link : Auto Layout Multiplier