Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCell height auto layout not working on iOS 10

I have a very simple UITableViewController, where I want to set a text on each textLabel of the UITableViewCells.

The height should be calculated by autolayout. Therefore, in my viewDidLoad set following values to the tableView properties:

tableView.estimatedRowHeight = UITableViewAutomaticDimension
tableView.rowHeight = UITableViewAutomaticDimension

This is my cellForRow method, where I initialize the default UITableViewCell and set the text to its label.

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        cell.textLabel?.numberOfLines = 0
        cell.textLabel?.text = "hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello hello"
        return cell
    }

My problem is, that the cell won't grow according to the size of the label. This issue only appears on iOS 10. On iOS 11 everything gets layed out as it should.

Please check following screenshots:

  • iOS 10

enter image description here

  • iOS 11

enter image description here

EDIT: 24th of May 2018

Having the textLabel only, it will work if there is an actual value on estimatedRowHeight.

But having a cell with style .subtitle, and setting a value to the descriptionLabel, the layouting won't work. Same issue: iOS 10 does not work. iOS 11 works (cell resizes according to both labels).

like image 526
kchromik Avatar asked May 24 '18 15:05

kchromik


1 Answers

This configuration from your viewDidLoad:

tableView.estimatedRowHeight = UITableViewAutomaticDimension
tableView.rowHeight = UITableViewAutomaticDimension

...works only on iOS 11. It is an iOS 11 innovation and does not apply to earlier systems.

For iOS 10, you must supply an actual estimatedRowHeight value (such as 60) to turn on automatic variable row height calculation.

like image 150
matt Avatar answered Nov 11 '22 02:11

matt