Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 8 auto-sizing UITableViewCell with UITextView

Tags:

iOS 8 introduced a way for tableViews to automatically adjust their cell's height based on their content (via AutoLayout).

// in viewDidLoad: tableView.rowHeight = UITableViewAutomaticDimension tableView.estimatedRowHeight = 44.0 

I already got it working with labels, images, etc.

However, I can not figure out a way for the table view cell to grow automatically as soon as a cell's text view changes?

Some more information on the setup: The UITextView inside a cell has horizontal and vertical constraints to the table view cell's contentView. I also disabled scrolling for the text view.

I have also tried to change the height constraint of the cell manually, but the cell would not adopt those changes as well.

like image 909
Alex Avatar asked Sep 11 '14 15:09

Alex


1 Answers

If everything is set up properly (Auto Layout constraints) you do not have to calculate anything yourself.

All you have to do is disable UITextView's scrolling enabled property then on textViewDidChange call tableView.beginUpdates() and tableView.endUpdates().

For a detailed explanation, check out a post I wrote which also includes a working sample project.

like image 171
Jure Avatar answered Oct 02 '22 04:10

Jure