Search code examples
iosuitableviewautolayoutnslayoutconstraintrow-height

iOS Auto-Layout: Dynamic height for table view cell


I have a table view with a bunch of cells (custom cell, which only has its content view).

In tableView:cellForRowAtIndexPath: I'm adding a predefined UIView (which has several subviews) to the content view of the custom cell. I set up all constraints for the UIView and its subviews before.

Last but not least, I set the vertical and horizontal constraints for the content view of my custom cell (superview) and the UIView, which was added before (subview).

The constraint strings look like this:

    H:|[view]|
    V:|[view]|

Unfortunately, I still get the default height for all table view cells. I'm wondering If there's a way to let auto layout do the calculation of the height automatically according to content size.


Solution

  • I solved the problem by using CGSize size = [view systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; in tableView:heightForRowAtIndexPath:.