UITableViewAutomaticDimension not working on iOS 8
Asked Answered
W

6

8

I was following this tutorial to make self sizing cells.

I registered custom cell into table view and in the cell xib I gave every subview constraints manually. Please refer to the source code in GitHub

Arranging 3 labels vertically in one cell's content view worked fine in iOS 9 but not in iOS 8 (both tested in device and simulator).

In iOS 8 one cell does not have fitting height and not every label shows all it's full text.

iOS 8 not fitting:

enter image description here

iOS 9 as I expected:

enter image description here

Any advice would be appreciated!

Update

code in viewDidLoad:

 tableView.registerNib(UINib.init(nibName: "SelfSizingCell", bundle: nil), forCellReuseIdentifier: cellIdentifier)
 tableView.dataSource = self
 tableView.delegate = self
 tableView.estimatedRowHeight = 60.0
 tableView.rowHeight = UITableViewAutomaticDimension
 tableView.reloadData()
Wane answered 4/1, 2016 at 3:27 Comment(7)
Did you set tableView.estimatedRowHeight in viewDidLoad?Cubiculum
@robmayoff yup I did, I made no difference to my code when tested it on iOS 8 and iOS 9. Please check the GitHub link above.Wane
I have to set specific height constraints for the upper 2 labels to make the height automation work in iOS8, or simply have no more than 2 labels in one cell. Looking for a better way ...Wane
@Wane Did you correct this problem ? I would like to know the solution as well. Please post an answer if you know. I am developing everything programmatically.Noaccount
@Maven the comment below the accepted answer worked for meWane
you have to set all left, right, bottom, and top constraints relative to cell container viewSeptarium
Swift 4.2/Xcode 10 UITableViewAutomaticDimension has been renamed to UITableView.automaticDimension self.tbl.estimatedRowHeight = 60.0 self.tbl.rowHeight = UITableView.automaticDimensionAlgernon
R
16

add following code in "cellForRowAtIndexPath" method before return cell.

    cell.setNeedsUpdateConstraints()
    cell.updateConstraintsIfNeeded()
Rollet answered 19/4, 2016 at 7:15 Comment(2)
What's causing this issue?Wane
even just cell.updateConstraintsIfNeeded() works...Clothesline
F
6

To make it work on iOS 8 you need 3 things:

1) Set your table "estimated row height" on your view did load method.

tableView.estimatedRowHeight = 200.0

2) Implement the "table view height for cell at... method". (Remove the 'override' if your class is not a subclass of 'UITableViewController' and you are adding the table view yourself.

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

3) Call the 'updateConstraintsIfNeeded' method when you return your cell inside the 'table view cell for row at...' method.

cell.updateConstraintsIfNeeded()

return cell

Notice: Step 3 is NOT needed for iOS 9+

Filibeg answered 15/12, 2016 at 9:17 Comment(0)
H
4

Give Top,Bottom,Leading,Trailing Contraints to UILabel used. Check numberOfLine must be 0 Then set below methods

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;}

Now run the project :)

Hardheaded answered 14/6, 2016 at 13:53 Comment(2)
What is the reason of that? Thanks.Contractive
For Xamarin use: GetHeightForRow with return UITableView.AutomaticDimension and EstimatedHeight with return UITableView.AutomaticDimensionBezonian
S
3

Version 10.1

UITableViewAutomaticDimension is renamed to UITableView.automaticDimension

Subtlety answered 25/12, 2018 at 16:51 Comment(0)
B
2

This two lines did the trick for me:

tableView.estimatedRowHeight = 200.0
tableView.rowHeight = UITableViewAutomaticDimension

Everything else I tried from other answers didn't work.

Byelection answered 23/11, 2017 at 13:43 Comment(0)
I
1

I can't make it work... I checked all hints in this thread but the table view cells don't resize their height.

I had the same custom table view cell set up as prototype cell in the table view in the storyboard and it worked.
For reasons of reusability, I set the cell up as xib. The table view loads and but the cells don't resize any more. It seems it works with storyboard but not with xib table view cells.
I have a UICollection view inside each tv cell with a varying number of cv cells, that's why the height of each tv cell cannot be the identical.

I didn't change any function calls, e.g. self.tableview.beginUpdates() and .endUpdates() is called in multiple places. I use Xcode 8.3.2

Ideologist answered 22/5, 2017 at 7:11 Comment(2)
issue resolved. the problem was that I had set a fixed height for the title view of the tv cell... It seems that none of the view elements that have constraints directly with the tv cell's content view can have any fixed height constraint. That is straightforward for the collection view in the tv cell but in my opinion not so straightforward for the title view... Very important for the collection view: self.collectionView.translatesAutoresizingMaskIntoConstraints = true .Ideologist
And since the collection view in the tv cell has scrolling switched off, I had created this extension to update its height every time the tv cell lays out it subviews: extension UICollectionView { func updateHeight() { let updatedHeight = self.collectionViewLayout.collectionViewContentSize.height if updatedHeight != self.frame.size.height { let frame = CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: self.frame.size.width, height: updatedHeight) self.frame = frame } } }Ideologist

© 2022 - 2024 — McMap. All rights reserved.