I have a tableViewCell
to which I am giving constraints programatically. I have only a single ImageView
inside the cell. Now I am not giving cell's contentView
a fixed height, rather I am giving the top
& bottom
of the imageView top contentView
so that the cell height increase with increase in imageView height.
messagingPersonProfilePicture.leadingAnchor.constraint(equalTo: self.leadingAnchor),
messagingPersonProfilePicture.topAnchor.constraint(equalTo: self.topAnchor, constant: 15),
messagingPersonProfilePicture.widthAnchor.constraint(equalToConstant: 50),
messagingPersonProfilePicture.heightAnchor.constraint(equalToConstant: 50),
messagingPersonProfilePicture.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -15),
Now, giving these constraints, the program is breaking these constraints telling me following:
"<NSLayoutConstraint:0x6000034a4e60 V:|-(15)-[UIImageView:0x7fa7f6605640] (active, names: '|':Paigham.MessagesTableViewCell:0x7fa7f7809c00'tableCellID' )>", "<NSLayoutConstraint:0x6000034a4f00 UIImageView:0x7fa7f6605640.height == 50 (active)>", "<NSLayoutConstraint:0x6000034a4f50 UIImageView:0x7fa7f6605640.bottom == Paigham.MessagesTableViewCell:0x7fa7f7809c00'tableCellID'.bottom - 15 (active)>", "<NSLayoutConstraint:0x600003494000 'UIView-Encapsulated-Layout-Height' Paigham.MessagesTableViewCell:0x7fa7f7809c00'tableCellID'.height == 80.5 (active)>"
Will attempt to recover by breaking constraint <NSLayoutConstraint:0x6000034a4f00 UIImageView:0x7fa7f6605640.height == 50 (active)>
Which I think is a bit strange as tableView cell is getting the proper height from its subviews.
But as soon as I set tableView
separatorStyle
to none
, the constraints aren't breaking anymore. What can be the reason behind that?
My tableViewMethods:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: tableCellID, for: indexPath) as! MessagesTableViewCell
cell.backgroundColor = .blue
return cell
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
My customCellCode:
class MessagesTableViewCell: UITableViewCell {
var messagingPersonProfilePicture: UIImageView = {
let v = UIImageView()
v.translatesAutoresizingMaskIntoConstraints = false
v.contentMode = .scaleAspectFill
v.image = #imageLiteral(resourceName: "unnamed-3")
v.layer.cornerRadius = 25
v.clipsToBounds = true
v.backgroundColor = .yellow
return v
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: .default, reuseIdentifier: reuseIdentifier)
setupMessageCell()
}
func setupMessageCell() {
contentView.addSubview(messagingPersonProfilePicture)
NSLayoutConstraint.activate([
messagingPersonProfilePicture.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
messagingPersonProfilePicture.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 15),
messagingPersonProfilePicture.widthAnchor.constraint(equalToConstant: 50),
messagingPersonProfilePicture.heightAnchor.constraint(equalToConstant: 50),
messagingPersonProfilePicture.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -15),
])
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
tableView.rowHeight = UITableView.automaticDimension
? – Mat