Ios – UITableViewCell’s contentView gets unwanted “height==44” constraint

autolayoutiosuitableview

I'm creating my UI entirely in code and using Masonry to constrain the cell's content view's subviews to the appropriate height. I'm using

[cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height

on iOS 7 for the row height, while iOS 8 handles it automatically.

Everything looks exactly as it should on screen, but in the console I get trainloads of warnings for conflicting constraints, which all seem to be caused by an unasked and unnecessary height constraint on the cell's content view (e.g. <NSLayoutConstraint UITableViewCellContentView.height == 44>).

On iOS 8 I'm setting the table view's rowHeight as UITableViewAutomaticDimension (effectively -1) but still I get this constraint. I'm only adding constraints between the content view and its own subviews, so no constraints between the content view and the cell itself.

Any idea where this constraint comes from and how to make it go away?

Edit: Now I actually found a "solution" of sorts – initially setting the content view's frame to something ridiculous, like CGRectMake(0, 0, 99999, 99999), before adding subviews or constraints, seems to make the warnings go away. But this doesn't exactly smell like the right way to do it, so can anyone tell of a better approach?

Best Answer

I had the same issue and fixed it setting the auto resizing mask of the cell like this:

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

Also in the controller I set the estimated height and tell the table view to use automatic dimension (in the viewDidLoad method:

    self.tableView.estimatedRowHeight = 120
    self.tableView.rowHeight = UITableView.automaticDimension

These links helped:

http://useyourloaf.com/blog/2014/08/07/self-sizing-table-view-cells.html

Auto layout constraints issue on iOS7 in UITableViewCell

Hope this helps!