如何检索 Swift 中约束常量的值?

How do I retrive the value of the constant of a constraint in Swift?

我有一个 NSLayoutConstraint 约束,如下所示:

var myConstant:CGFloat = 20

var myConstraint = NSLayoutConstraint (item: image,
        attribute: NSLayoutAttribute.Width,
        relatedBy: NSLayoutRelation.Equal,
        toItem: nil,
        attribute: NSLayoutAttribute.NotAnAttribute,
        multiplier: 1,
        constant: myConstant)
    self.view.addConstraint(myConstraint)

按下 UIButton 时,图像会调整大小,因此图像的常量会发生变化。

self.image.transform = CGAffineTransformMakeScale(0.8, 0.8)

不过,当我在转换完成后打印常量的值时,常量与之前的值保持不变:

println(constraintImageCharacterHomeLeading.constant) // 20

为什么会这样?我调整了图像的大小,因此约束必须有所不同。

myConstraint.constant

它将为您完成这项工作。

转换和自动布局不能很好地协同工作。如果您设置视图的变换,它的预期行为是不更新约束。