I am writing a programatic table view with custom cells for an iPad only app, I have the data coming through and the layout almost there but I am struggling with the widths of UILabel in the UITableViewCell I have content overlapping.
The image at the start of the cell is an know width and height 128 x 64 and so is the last image, QR Code, which is 64 x 64
What I am trying to do is using constraints is make the three UILabel between the two UIImage views layout in a n equal manner with the first contentTitleLabel occupying the most space and the next two occupying an equal space each. Hopefully removing the overlapping I am seeing
This is my table cell code which contains the constraints, I am aware that it's not best practice to set hardcoded widths but I am at a point where I cannot work this out.
import UIKit
class CustomCell: UITableViewCell {
var contentImageView = UIImageView()
var contentTitleLabel = UILabel()
var contentAuthorLabel = UILabel()
var contentDetailsLabel = UILabel()
var contentQRCodeView = UIImageView()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
print(style)
addSubview(contentImageView)
addSubview(contentTitleLabel)
addSubview(contentAuthorLabel)
addSubview(contentDetailsLabel)
addSubview(contentQRCodeView)
configureImageView()
configureTitleLabel()
configureAuthorLabel()
configureDetailsLabel()
configureQRCodeView()
setImageConstraints()
setTitleLabelConstraints()
setAuthorLabelConstraints()
setDetailsLabelConstraints()
setQRCodeConstraints()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func set(content: ContentCellItem) {
contentImageView.image = content.image
contentTitleLabel.text = content.name
contentAuthorLabel.text = content.author
contentDetailsLabel.text = content.details
contentQRCodeView.image = content.qrCode
}
func configureImageView() {
contentImageView.layer.cornerRadius = 10
contentImageView.clipsToBounds = true
}
func configureTitleLabel() {
contentTitleLabel.numberOfLines = 0
contentTitleLabel.adjustsFontSizeToFitWidth = true
}
func configureAuthorLabel() {
contentAuthorLabel.numberOfLines = 0
//contentAuthorLabel.adjustsFontSizeToFitWidth = true
}
func configureDetailsLabel() {
contentDetailsLabel.numberOfLines = 0
//contentDetailsLabel.adjustsFontSizeToFitWidth = true
}
func configureQRCodeView() {
contentQRCodeView.layer.cornerRadius = 10
contentQRCodeView.clipsToBounds = true
}
func setImageConstraints() {
contentImageView.translatesAutoresizingMaskIntoConstraints = false
contentImageView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
contentImageView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20).isActive = true
contentImageView.heightAnchor.constraint(equalToConstant: 64).isActive = true
contentImageView.widthAnchor.constraint(equalToConstant: 128).isActive = true
}
func setTitleLabelConstraints() {
contentTitleLabel.translatesAutoresizingMaskIntoConstraints = false
contentTitleLabel.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
contentTitleLabel.leadingAnchor.constraint(equalTo: contentImageView.trailingAnchor, constant: 20).isActive = true
contentTitleLabel.heightAnchor.constraint(equalToConstant: 64).isActive = true
contentTitleLabel.widthAnchor.constraint(equalToConstant: 220).isActive = true
//contentTitleLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -20).isActive = true
}
func setAuthorLabelConstraints() {
contentAuthorLabel.translatesAutoresizingMaskIntoConstraints = false
contentAuthorLabel.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
contentAuthorLabel.leadingAnchor.constraint(equalTo: contentTitleLabel.trailingAnchor, constant: 20).isActive = true
contentAuthorLabel.heightAnchor.constraint(equalToConstant: 64).isActive = true
contentAuthorLabel.widthAnchor.constraint(equalToConstant: 50).isActive = true
//contentAuthorLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -20).isActive = true
}
func setDetailsLabelConstraints() {
contentDetailsLabel.translatesAutoresizingMaskIntoConstraints = false
contentDetailsLabel.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
contentDetailsLabel.leadingAnchor.constraint(equalTo: contentAuthorLabel.trailingAnchor, constant: 20).isActive = true
contentDetailsLabel.heightAnchor.constraint(equalToConstant: 64).isActive = true
contentDetailsLabel.widthAnchor.constraint(equalToConstant: 50).isActive = true
//contentDetailsLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -20).isActive = true
}
func setQRCodeConstraints() {
contentQRCodeView.translatesAutoresizingMaskIntoConstraints = false
contentQRCodeView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
contentQRCodeView.leadingAnchor.constraint(equalTo: contentDetailsLabel.leadingAnchor, constant: 20).isActive = true
contentQRCodeView.heightAnchor.constraint(equalToConstant: 64).isActive = true
contentQRCodeView.widthAnchor.constraint(equalToConstant: 64).isActive = true
contentQRCodeView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -20).isActive = true
}
}

