我有一个 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
为什么会这样?我调整了图像的大小,因此约束必须有所不同。
最佳答案
Transforms 和 Auto Layout 不能很好地协同工作。如果您设置 View 的转换,它的预期行为是不更新约束。
关于ios - 如何在 Swift 中检索约束常量的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28629076/