Set tableView scrolling property to false.
First Approach:
-
Create a subclass for tableView and override intrinsicContentSize.
class MyOwnTableView: UITableView { override var intrinsicContentSize: CGSize { self.layoutIfNeeded() return self.contentSize } override var contentSize: CGSize { didSet{ self.invalidateIntrinsicContentSize() } } override func reloadData() { super.reloadData() self.invalidateIntrinsicContentSize() } }
-
In Interface builder change the class of your tableView to MyOwnTableView (subclass UITableView).
-
Set automatic row height for the table view.
tableView.estimatedRowHeight = 60.0; tableView.rowHeight = UITableViewAutomaticDimension;
Second Approach:
1. Create a height constraint with any value for tableView and connect an IBOutlet that sets the tableView height.
-
Set the height constraint’s constant to tableView.contentSize.height after reloading the data.
self.tableViewHeight.constant = self.tableView.contentSize.height