简体   繁体   中英

How can I change the height of an UIImageView programmatically?

I am teaching myself Swift through taking a hands-on approach. I have a simple image that has a height constraint with a constant of 150 set in the storyboard. However I would like to change it to 70 programmatically.

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

I have access to the image by doing tableElement.BigImage.image , and I would like to find the height property of it and set it to 70. I have tried this tableElement.BigImage.image.topCapHeight = 70 however that gives an error. How can I do this?

This is my outlet to it

    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
    }

}

I am trying to learn how to change the height of the image programmatically because I am getting 2 different batches of images and I want to separate them by height sort of a red/blue thing.

Double-click to edit constraint. Set identifier for it. 在此处输入图片说明

And find it programmatically.

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

Update
As you created a custom cell, to add another outlet is better. CTRL-DRAG an outlet, it will look like this.

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
    }

}

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