Hello:
As you will guess from what you are about to read I am extremely new at swift and IOS coding. I have am trying to display content on a UITableview using a UITableViewController and a custom coded UITableViewCell. In this cell I have two items, an imageview and a label. Both get populated with their corresponding data programmatically. The problem that I am having is laying out the image and the label inside the customcell class the way that I want. More specifically, I'm having problems figuring out what is driving the height of the custom cell's row when the project runs. Ideally I'd like the imageview to set this height constraint (along with the width), regardless of the size of the original image asset. But when I set the imageview's height constraints I get an error in the debugger telling me that it is "Unable to simultaneously satisfy constraints" and it mentions the constraimts that I have set for the imageview's bottom & height in the error code accordingly. This is as close as I have been able to run the project to make it look like I want but I keep getting that error.
I read somewhere that in order to have autolayout work properly in code I have to set the imageview's leading, top and bottom constraints somehow. But if I do not set a height constraing for the imageview then the table view uses the original height for the image and throws off the look of the table. These are the snippets of code that I have that I think are relevant:
code inside my TableviewController "viewDidLoad()" function
self.tableView.rowHeight = UITableView.automaticDimension
self.tableView.estimatedRowHeight = 200
code inside the custom "UITableViewCell" class declaration
var messageLabel: UILabel = {
var label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.backgroundColor = .blue // REMOVE ME JUST FOR TESTING
label.textColor = .black
label.font = UIFont(name: "Verdana", size: 20)
return label
}()
var mainImageView: UIImageView = {
var imageView = UIImageView()
imageView.contentMode = .center // image will never be stretched veritically or horizontally
imageView.clipsToBounds = true
imageView.translatesAutoresizingMaskIntoConstraints = false //enable auto layout
imageView.layer.cornerRadius = imageView.frame.size.width / 2
imageView.backgroundColor = .red // REMOVE ME JUST FOR TESTING
return imageView
}()
code inside the "override init(style:UITableViewCell.CellStyle, reuseIdentifier: String?)" function
super.init(style: style, reuseIdentifier: reuseIdentifier)
//add the views that we have created to this cell
self.contentView.addSubview(mainImageView)
self.contentView.addSubview(messageLabel)
//add constraints for each of the views
mainImageView.topAnchor.constraint(equalTo: self.contentView.topAnchor).isActive = true
mainImageView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor).isActive = true
mainImageView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor).isActive = true //dynamic row height also requires bottom constraints
mainImageView.widthAnchor.constraint(equalToConstant: 100).isActive = true
messageLabel.topAnchor.constraint(equalTo: self.contentView.topAnchor).isActive = true
messageLabel.leadingAnchor.constraint(equalTo: mainImageView.trailingAnchor, constant: 5).isActive = true
messageLabel.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor).isActive = true
One of the things that I tried was to test the results by commenting out the "mainImageView" bottom constrain and just scpecify a height constaint for the imageview. But the result was that the images overlapped ontop of each other and the text labels' heights did not match the images.
I don't know what I am missing. What am I doing wrong or not doing?
Thanks.