Reputation: 1091
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.
Upvotes: 0
Views: 542
Reputation: 6379
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
}
}
Upvotes: 1