UITableView max size while respecting autolayout constraints

241 views Asked by At

So what i want to do is to have a tableview that maxes out it sizes while respecting autolayout constraints. Right now i have a tableview that maxes out it is size, but its not scrollable.

public class ExpandingTableView2: UITableView {

    override public func reloadData() {
        super.reloadData()
        self.setNeedsLayout()
        self.invalidateIntrinsicContentSize()
    }

    override public func layoutSubviews() {
        super.layoutSubviews()
        if !self.bounds.size.equalTo(self.intrinsicContentSize) {
            self.invalidateIntrinsicContentSize()

        }
    }

    override public var intrinsicContentSize: CGSize {
        self.layoutIfNeeded()
        let intrinsicContentSize = super.contentSize
        return intrinsicContentSize
    }
}
2

There are 2 answers

8
Shruti On BEST ANSWER

You can calculate the tableview height based on the content size by creating an outlet for height contraint:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if tableViewHeightConstraint.constant != tableView.contentSize.height && tableViewHeightConstraint.constant < view.frame.height // This will prevent to increase tableview height beyond the view and let it scroll
{
        setupHeightConstraintForTableView(tableView.contentSize.height)
    }

   // rest of the code
}

func setupHeightConstraintForTableView(_ heightValue: CGFloat) {
    tableViewHeightConstraint.constant = heightValue
    self.updateConstraintsIfNeeded()
}
0
Tommy Sadiq Hinrichsen On

What is important here is to keep the constraints that govern the max height of the tableview higher than the constraints used to set its height. I also set the content compression resistance of the tableview to 250.

public class ExpandingTableView: UITableView {

    @IBOutlet public weak var heightConstraint: NSLayoutConstraint?

    override public func reloadData() {

        guard let heightConstraint = self.heightConstraint, heightConstraint.priority.rawValue < 750  else { return }

        var height: CGFloat = 0.0
        for section in 0..<(self.dataSource?.numberOfSections?(in: self) ?? 1) {
            for row in 0..<(self.dataSource?.tableView(self, numberOfRowsInSection: section) ?? 0) {
                let rowHeight = (self.delegate?.tableView?(self, heightForRowAt: IndexPath(row: row, section: section))) ?? self.rowHeight
                height += rowHeight
            }
        }

        heightConstraint.constant = height

        super.reloadData()
        self.setNeedsLayout()
    }

}