在Swift 3中以编程方式设置UIImageView AspectRatio约束 [英] Set UIImageView AspectRatio constraint programmatically in swift 3

查看:297
本文介绍了在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)

这篇关于在Swift 3中以编程方式设置UIImageView AspectRatio约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆