WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

Self Sizing Cell using Multiple Stack Views

Forums > iOS

@eak65  

I've looked all over the forum and attempted all the solutions and thus far nothing has worked. I noticed my UIImageView was overlaying multiple cells, meaning the celll did not automatically adjust its height. Here is the constraint i found in the console it complained about.

"<NSLayoutConstraint:0x600001970f50 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7f86a4813dd0.height == 44 (active)>"

In my tableViewController I have the follow

  tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 300

Here is my entire cell that should self size.

//
//  UserConnectCell.swift
//
//

import UIKit

class UserConnectionCell: UITableViewCell {

    fileprivate let leftImageView: UIImageView = {
        let uiImageView = UIImageView()
        uiImageView.translatesAutoresizingMaskIntoConstraints = false

       return uiImageView
    }()

    fileprivate let leftLabel: UILabel = {
        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()

    fileprivate let middleLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 0
        label.font = UIFont(name: "Ariel", size: 10)
        label.textAlignment = .center
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()

    fileprivate let rightImageView: UIImageView = {
        let uiImageView = UIImageView()
        uiImageView.translatesAutoresizingMaskIntoConstraints = false
       return uiImageView
    }()

    fileprivate let rightLabel: UILabel = {
        let label = UILabel()
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()

    fileprivate let stackViewLeft: UIStackView = {
        let stackView = UIStackView()
        stackView.axis = .vertical
        stackView.translatesAutoresizingMaskIntoConstraints = false
        return stackView
    }()
    fileprivate let stackViewRight: UIStackView = {
        let stackView = UIStackView()
        stackView.axis = .vertical
        stackView.translatesAutoresizingMaskIntoConstraints = false
        return stackView
    }()
    fileprivate let stackViewMain: UIStackView = {
        let stackView = UIStackView()
        stackView.axis = .horizontal
        stackView.alignment = .fill
        stackView.spacing = 0
        stackView.translatesAutoresizingMaskIntoConstraints = false
        return stackView
    }()

    //
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier:reuseIdentifier)

        stackViewLeft.addArrangedSubview(leftImageView)
        stackViewLeft.addArrangedSubview(leftLabel)

        stackViewRight.addArrangedSubview(rightImageView)
        stackViewRight.addArrangedSubview(rightLabel)

        stackViewMain.addArrangedSubview(stackViewLeft)
        stackViewMain.addArrangedSubview(middleLabel)
        stackViewMain.addArrangedSubview(stackViewRight)

        contentView.addSubview(stackViewMain)
    }
    // called when trying to layout subviews.
    override func layoutSubviews() {
        super.layoutSubviews()

        stackViewLeft.addConstraint(NSLayoutConstraint(item: leftImageView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1, constant: 100))
        stackViewLeft.addConstraint(NSLayoutConstraint(item: leftImageView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: 100))

        stackViewRight.addConstraint(NSLayoutConstraint(item: rightImageView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1, constant: 100))
        stackViewRight.addConstraint(NSLayoutConstraint(item: rightImageView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1, constant: 100))

        NSLayoutConstraint.activate(
            [stackViewMain.topAnchor.constraint(equalTo: contentView.topAnchor,constant: 0),
             stackViewMain.leadingAnchor.constraint(equalTo: contentView.leadingAnchor,constant: 0),
             stackViewMain.trailingAnchor.constraint(equalTo: contentView.trailingAnchor,constant: 0),
             stackViewMain.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 0)
            ])

    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    var viewModel : UserConnectionViewModel? {
        didSet {
            // move this to the view model
            if let profileUrl = viewModel?.leftImageUrl {
                leftImageView.loadImageFromURL(url: profileUrl)
            } else {
                leftImageView.image = UIImage(named: "defaultprofile")
            }

            if let profileUrl = viewModel?.rightImageUrl {
                rightImageView.loadImageFromURL(url: profileUrl)
            } else {
                rightImageView.image = UIImage(named: "defaultprofile")
            }
            leftLabel.text = viewModel?.leftLabel
            middleLabel.text = viewModel?.middleLabel
            rightLabel.text = viewModel?.rightlabel
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        self.contentView.autoresizingMask = .flexibleHeight
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

Any suggestions on how to fix this issue?

1      

Save 50% in my Black Friday sale.

SAVE 50% To celebrate WWDC22, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.