简体   繁体   中英

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

I have a NSLayoutConstraint constraint that looks like this:

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)

When an UIButton is pressed, the image gets resized, therefore the constant of the image changes.

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

Though, when I print the value of the constant after the transformation finished, the constant stays the same as it was before:

println(constraintImageCharacterHomeLeading.constant) // 20

Why is that happening? I resized the image, hence the constraint must vary.

myConstraint.constant

它将为您完成工作。

Transforms and Auto Layout don't work well together. If you set the transform of a view, it is expected behavior for it to NOT update the constraint.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM