UITableView Content Size Smaller than Expected

1.6k views Asked by At

I have a UIScrollView with a UITableView inside. I have disabled scrolling on the table view. I am using the following code to keep the UITableView's height to the height of it's content.

override func updateViewConstraints() {
        self.editView!.editTblHeightCnst.constant = self.editView!.editTbl.contentSize.height + self.editView!.editTbl.contentInset.top + self.editView!.editTbl.contentInset.bottom + self.editView!.editTbl.contentOffset.y
        super.updateViewConstraints()
    }

However, the content size I'm getting there is too small. If I add a couple hundered or less to the content size, it works. Why is my content size not what I'm expecting and how do I fix this?

1

There are 1 answers

0
Yohan Dahmani On

I found a solution, write this before doing all your work :

tableView.estimatedRowHeight = 0;

In iOS 11, table views use estimated heights by default. This means that the contentSize is just as estimated value initially. If you need to use the contentSize, you’ll want to disable estimated heights by setting the 3 estimated height properties to zero: tableView.estimatedRowHeight = 0 tableView.estimatedSectionHeaderHeight = 0 tableView.estimatedSectionFooterHeight = 0

source : https://forums.developer.apple.com/thread/81895