2

This is the tabel view cell with the image view

class ChatTableViewCell: UITableViewCell {

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


        self.contentView.addSubview(userImageView)

        userImageView_constraints()

        }

        var userImageView: UIImageView = {
        let imageView = UIImageView()
        let imageViewHeightAndWidth: CGFloat = 55
        let image = UIImage(named: "steve")
        imageView.image = image
        imageView.clipsToBounds = true
        imageView.layer.cornerRadius = imageViewHeightAndWidth / 2
        imageView.translatesAutoresizingMaskIntoConstraints = false
        return imageView
    }()


func userImageView_constraints(){
    userImageView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 20).isActive = true
    userImageView.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: 0).isActive = true
    userImageView.widthAnchor.constraint(equalToConstant: 55).isActive=true
    userImageView.heightAnchor.constraint(equalToConstant: 55).isActive=true

}

and heres the code for the table view

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "chatCell", for: indexPath) as! ChatTableViewCell
        return cell
    }

And this is the error that shows in the console

[Warning] Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a table view cell's content view. We're considering the collapse unintentional and using standard height instead. Cell: >

j.doe
  • 305
  • 3
  • 16
  • Add a bottom constraint from the bottom of the image view to content view's bottom anchor. If needed, you can give it a low priority to allow other content to effect the height – MadProgrammer Oct 17 '19 at 23:30
  • That just breaks my imageview constraint Will attempt to recover by breaking constraint – j.doe Oct 17 '19 at 23:52
  • shouldnt topconstraint with height be enought for the tableview cell to calculate height – j.doe Oct 17 '19 at 23:52
  • The content view needs to be resolved, so you need constraints from the top to the bottom, from that the overall height can be calculated – MadProgrammer Oct 17 '19 at 23:56
  • but its still breaking the constraint – j.doe Oct 17 '19 at 23:58
  • [Example](https://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights), [example](https://www.raywenderlich.com/8549-self-sizing-table-view-cells), [example](http://www.thomashanning.com/uitableview-automatic-row-height/), [example](https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithSelf-SizingTableViewCells.html), [example](https://www.sitepoint.com/self-sizing-cells-uitableview-auto-layout/). Also don't forget to set `estimatedRowHeight` – MadProgrammer Oct 17 '19 at 23:58
  • *"but its still breaking the constraint"* - Did you set the bottom constraints priority lower than 1000? I usually use 999. Are you using `estimatedRowHeight` in the `UITableView`? – MadProgrammer Oct 18 '19 at 00:00
  • setting the priority resolved it. Thanks! those examples are very helpful! – j.doe Oct 18 '19 at 00:51

1 Answers1

0

You can use contentView, it's not strictly necessary, I use straight self.addSubview()

Since the height of your image is greater than the height of a standard row. You either have to implement a height with UITableViewDelegate method heightForRow where the height is greater than your 55 or you to add a bottom anchor: userImageView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true

Alexander
  • 1,424
  • 18
  • 23