2

Already read Auto-Layout: Get UIImageView height to calculate cell height correctly,the answer works well in ios 8 with UITableViewAutomaticDimension, however it is not suitable for lower ios version.

I have created a dynamic height UITableView with systemLayoutSizeFittingSize.

There is a square UIImageView in each cell, I want UIImageView keep square in both iphone 5 and iphone 6/plus. So I set constraints to the imageview as : enter image description here

The image view's height is set aspect ratio 1:1 to its width, and both the constraints on leading and trailing keep the imageview's width is equal to the cell. The bottom space from the imageview's bottom to the content view's bottom is 29 pixel.

Moreover, I wrote the code in the tableview delegate like

override  func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
     let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
     return cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height+1;
}

However, the result is not as same as what I predicate, the imageview is very long :

the red color is the background of the image view.

So what's going on?

Community
  • 1
  • 1
Exia
  • 2,381
  • 4
  • 17
  • 24
  • Make sure the image views masks to bounds is set to true, also known as clipsToBounds depending on whether you want to do it in IB or code – Daniel Galasko Dec 26 '14 at 14:20
  • I have already set clipsToBounds, but I think the problem is about the systemLayoutSizeFittingSize method is not correct figure the height for the cell. – Exia Dec 26 '14 at 17:03
  • When heightForRowAtIndexPath is called, has iOS already computed the size of what is going into the contentView? – Mike Taverne Dec 26 '14 at 18:58
  • TO Mike Taverne: Since heightForRowAtIndexPath is a delegate method, ios UITableView still does not know what's the real height of such cell until the delegate method return a height. – Exia Dec 26 '14 at 20:01

0 Answers0