7

How can I programmatically change the multiplier in the simplest way?

enter image description here

For Swift 2.0

Cœur
  • 37,241
  • 25
  • 195
  • 267
Jams
  • 473
  • 2
  • 6
  • 18
  • multiplier is readonly, for animations and programatic adjustments on constraints at runtime, playing with constant is advised as i know. – meth Jan 20 '16 at 22:56
  • did you try solutions from here http://stackoverflow.com/questions/19593641/can-i-change-multiplier-property-for-nslayoutconstraint ? – Bretsko Jan 20 '16 at 23:03

3 Answers3

13

So for Y, if you set the top of the image equal with a constant of 0 to the top of the superView. then enter this code:

@IBOutlet weak var topc: NSLayoutConstraint!


let heightOfSuperview = self.view.bounds.height
topc.constant = heightOfSuperview * 0.90 // this has the same effect as multiplier  

This is equivalent of Center.y multiplier = 0.9:1

Jams
  • 473
  • 2
  • 6
  • 18
  • You are the best! I was trying to broke the wall with my head and you give me the pill to the headache :) – Francisco Romero Oct 21 '16 at 12:07
  • Except this doesn't work since it is **constant**, as the accessor suggests. If you resize a view this measurement won't adjust proportionally, like a `multiplier` does. –  May 26 '21 at 07:58
1

I try to use extension but it not work, but change to the global utility function and it work for me:

public class func changeMultiplier(constraint: NSLayoutConstraint, multiplier: CGFloat) -> NSLayoutConstraint {
    let newConstraint = NSLayoutConstraint(
        item: constraint.firstItem,
        attribute: constraint.firstAttribute,
        relatedBy: constraint.relation,
        toItem: constraint.secondItem,
        attribute: constraint.secondAttribute,
        multiplier: multiplier,
        constant: constraint.constant)

    newConstraint.priority = constraint.priority
    NSLayoutConstraint.deactivateConstraints([constraint])
    NSLayoutConstraint.activateConstraints([newConstraint])
    return newConstraint
}
Huy Vu
  • 404
  • 3
  • 12
0

Simple answer, no extensions required. I tried for my case, worked fine for me.

So as multiplier is a get only property, we can simply set multiplier in the following way :

yourConstraintOutlet.setValue(yourDesiredMultiplierValue, forKey: "multiplier")

yourConstraintOutlet.setValue(0.75, forKey: "multiplier")

No need to write extensions or multiply by height of superview.

Ashish Bahl
  • 1,482
  • 1
  • 18
  • 27
  • This is no working after the constraint has become active – mcscxv May 10 '22 at 17:36
  • you have to write yourConstraintOutletName.setValue(yourMultiplierValue, forKey: "multiplier") – Ashish Bahl May 11 '22 at 15:27
  • libc++abi: terminating with uncaught exception of type NSException *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unable to change the multiplier after a constraint has been added to a view' terminating with uncaught exception of type NSException – pkc456 Apr 22 '23 at 17:31