Yoomama
Yoomama

Reputation: 143

Autolayout UITableViewCells covering up bottom

I'm having trouble with this UITableViewCell. I'm getting strings from a server and inserting it in viewDidLoad, but the bottom of the cell, namely summaryLabel is being covered up. Is it the inset? Here's the code:

class SummaryTableViewCell: UITableViewCell {
    let titleLabel = UILabel()
    let createdLabel = UILabel()
    let summaryLabel = UILabel()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        titleLabel.numberOfLines = 2
        titleLabel.lineBreakMode = .byWordWrapping
        titleLabel.font = UIFont.boldSystemFont(ofSize: 23)

        summaryLabel.numberOfLines = 4
        summaryLabel.lineBreakMode = .byWordWrapping

        contentView.clipsToBounds = true
        titleLabel.translatesAutoresizingMaskIntoConstraints = false
        createdLabel.translatesAutoresizingMaskIntoConstraints = false
        summaryLabel.translatesAutoresizingMaskIntoConstraints = false

        contentView.addSubview(titleLabel)
        contentView.addSubview(createdLabel)
        contentView.addSubview(summaryLabel)
        let lg = contentView.safeAreaLayoutGuide
        NSLayoutConstraint.activate([
            titleLabel.topAnchor.constraint(equalTo: lg.topAnchor),
            titleLabel.leadingAnchor.constraint(equalTo: lg.leadingAnchor),
            titleLabel.trailingAnchor.constraint(equalTo: lg.trailingAnchor),
            titleLabel.bottomAnchor.constraint(equalTo: createdLabel.topAnchor),

            createdLabel.leadingAnchor.constraint(equalTo: lg.leadingAnchor),
            createdLabel.trailingAnchor.constraint(equalTo: lg.trailingAnchor),
            createdLabel.bottomAnchor.constraint(equalTo: summaryLabel.topAnchor, constant: -8),

            summaryLabel.leadingAnchor.constraint(equalTo: lg.leadingAnchor),
            summaryLabel.trailingAnchor.constraint(equalTo: lg.trailingAnchor),
            summaryLabel.bottomAnchor.constraint(equalTo: lg.bottomAnchor)
        ])
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        contentView.frame = contentView.frame.inset(by: UIEdgeInsets(top: 15, left: 17, bottom: 15, right: 17))
    }
    required init?(coder aDecoder: NSCoder) {super.init(coder: aDecoder)}
}

Upvotes: 1

Views: 59

Answers (2)

DonMag
DonMag

Reputation: 77690

Another option, which can save some effort and can make it easier to adjust if you decide to:

contentView.layoutMargins = UIEdgeInsets(top: 15, left: 17, bottom: 15, right: 17)
let lg = contentView.layoutMarginsGuide

Then you can leave your constraints as-is (without needing to set all the constants), and you can remove your layoutSubviews() func completely.

Upvotes: 1

Shehata Gamal
Shehata Gamal

Reputation: 100549

Don't mix autolayout with frame layout represented by setting the frame of contentView , so Substitue with constant

titleLabel.topAnchor.constraint(equalTo: lg.topAnchor,constant:15), 
summaryLabel.bottomAnchor.constraint(equalTo: lg.bottomAnchor,constant:-15)

and change leading & trailing for all lbls to something like this

titleLabel.leadingAnchor.constraint(equalTo: lg.leadingAnchor,constant:17),
titleLabel.trailingAnchor.constraint(equalTo: lg.trailingAnchor,constant:-17), 

Upvotes: 2

Related Questions