繁体   English   中英

如何以编程方式更改UIImageView的高度?

[英]How can I change the height of an UIImageView programmatically?

我正在通过动手实践教自己Swift。 我有一个简单的图像,该图像在情节提要板上设置了150的常数限制。 但是我想通过编程将其更改为70。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let tableElement = tableView.dequeueReusableCell(withIdentifier: "FirstTable", for: indexPath) 
    tableElement.BigImage.image = ?? 
    return tableElement
}

我可以通过执行tableElement.BigImage.image来访问该图像,并且我想找到它的height属性并将其设置为70。我已经试过了这个tableElement.BigImage.image.topCapHeight = 70但这给出了错误。 我怎样才能做到这一点?

这是我的出路

    class myNewCell: UITableViewCell {

    @IBOutlet weak var BigImage: UIImageView!


    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

我正在尝试学习如何以编程方式更改图像的高度,因为我要获取2个不同批次的图像,并且我想按红色/蓝色物体的高度将它们分开。

双击以编辑约束。 为其设置标识符。 在此处输入图片说明

并以编程方式找到它。

for constraint in UIView().constraints {
    if constraint.identifier == "your identifier" {
        // set here
    }
}

更新资料
创建自定义单元时,添加另一个插座更好。 CTRL-DRAG一个插座,它将看起来像这样。

class myNewCell: UITableViewCell {

    @IBOutlet weak var BigImage: UIImageView!
    @IBOutlet weak var topCapHeight: NSLayoutConstraint!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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