HomeiOS Developmentios - Cell content material constraints programmatically

ios – Cell content material constraints programmatically


I’m making an attempt to redo my venture programmatically with no storyboard. I’ve a tableView with a prototype cell.

The cell incorporates an imageView (briefcase), firstLabel (on the high) and secondLabel (proper below the firstLabel). I attempted to arrange constraints, however for some motive secondLabel does not present in any respect, and the cell top does not modify to the content material top routinely.

This is the code for the cell:

import UIKit

class Cell: UITableViewCell {

    let briefcaseImage: UIImageView = {
        let img = UIImageView(picture: UIImage(systemName: "briefcase"))
        img.contentMode = .scaleAspectFill
        img.tintColor = UIColor(crimson: 0.20, inexperienced: 0.68, blue: 0.90, alpha: 1.00)
        img.translatesAutoresizingMaskIntoConstraints = false
        img.clipsToBounds = true
        return img }()

    let firstLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 0
        label.lineBreakMode = .byWordWrapping
        label.font = UIFont.systemFont(ofSize: 17)
        label.translatesAutoresizingMaskIntoConstraints = false
        return label }()

    let secondLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 0
        label.lineBreakMode = .byWordWrapping
        label.textAlignment = .proper
        label.font = UIFont.systemFont(ofSize: 15)
        label.translatesAutoresizingMaskIntoConstraints = false
        return label }()

    override init(model: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        tremendous.init(model: model, reuseIdentifier: reuseIdentifier)
        self.contentView.addSubview(briefcaseImage)
        self.contentView.addSubview(firstLabel)
        self.contentView.addSubview(secondLabel)
    
        //MARK: Constraints for the briefcaseImage
        briefcaseImage.topAnchor.constraint(equalTo: topAnchor, fixed: 11).isActive = true
        briefcaseImage.leadingAnchor.constraint(equalTo: leadingAnchor, fixed: 16).isActive = true
        briefcaseImage.heightAnchor.constraint(equalToConstant: 22.5).isActive = true
        briefcaseImage.widthAnchor.constraint(equalToConstant: 20.0).isActive = true
    
        //MARK: Constraints for the firstLabel
        firstLabel.topAnchor.constraint(equalTo: topAnchor, fixed: 11).isActive = true
        firstLabel.leadingAnchor.constraint(equalTo: briefcaseImage.trailingAnchor, fixed: 10).isActive = true
        firstLabel.trailingAnchor.constraint(equalTo: trailingAnchor, fixed: -16).isActive = true
    
        //MARK: Constraints for the secondLabel
        secondLabel.topAnchor.constraint(equalTo: firstLabel.bottomAnchor, fixed: 4.5).isActive = true
        secondLabel.leadingAnchor.constraint(equalTo: briefcaseImage.trailingAnchor, fixed: 10).isActive = true
        secondLabel.trailingAnchor.constraint(equalTo: trailingAnchor, fixed: -16).isActive = true
        secondLabel.bottomAnchor.constraint(equalTo: bottomAnchor, fixed: -11).isActive = true
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been applied")
    }
}

Additionally, once I run the app, I get this error within the debugger part:

Detected a case the place constraints ambiguously counsel a top of zero for a desk view cell's content material view. We're contemplating the collapse unintentional and utilizing customary top as an alternative.

That is how the cell seems like (not what I would like):

enter image description here

How do I repair my constraints correctly in order that the secondLabel reveals and the cell top autoadjusts to the content material top?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments