Programmatically change the center alignment limit factor of X / Y

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

enter image description here

For Swift 2.0

+5
source share
2 answers

So, for Y, if you set the top of the image equal to the constant 0 at the top of the supervisor. 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 the equivalent of Center.y = 0.9: 1

+6
source

I try to use the extension, but it does not work, but it changes the function of the global utility, and it works 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
}
+1
source

Source: https://habr.com/ru/post/1625227/


All Articles