以编程方式更改中心X / Y的对齐约束的乘数

时间:2016-01-20 22:44:20

标签: ios swift constraints

如何以最简单的方式以编程方式更改乘数?

enter image description here

对于Swift 2.0

2 个答案:

答案 0 :(得分:6)

因此,对于Y,如果将图像的顶部设置为等于0的常量到superView的顶部。然后输入以下代码:

@IBOutlet weak var topc: NSLayoutConstraint!


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

这相当于Center.y乘数= 0.9:1

答案 1 :(得分:1)

我尝试使用扩展但不起作用,但更改为全局实用程序功能,它对我有用:

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
}