在Swift 3中以编程方式设置UIImageView AspectRatio约束
问题描述:
我在Storyboard中有一个UIImageView,纵横比为1:1,在某些情况下,我想在ViewController中以编程方式将其更改为2:1.我在ViewController中创建了该约束的引用,但无法设置约束.
I have an UIImageView in storyboard which AspectRatio is 1:1, that I want to change to 2:1 programmatically in ViewController in some cases. I create reference of that constraint in ViewController but unable to set the constraint.
答
您可以通过编程方式在迅速3中更改约束
You can change constraint programmatically in swift 3
let aspectRatioConstraint = NSLayoutConstraint(item: self.YourImageObj,attribute: .height,relatedBy: .equal,toItem: self.YourImageObj,attribute: .width,multiplier: (2.0 / 1.0),constant: 0)
self.YourImageObj.addConstraint(aspectRatioConstraint)