Make UITableView not scrollable and adjust height to accommodate all cells

Set tableView scrolling property to false.

First Approach:

  1. 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()
        }
    }
    
  2. In Interface builder change the class of your tableView to MyOwnTableView (subclass UITableView).

  3. 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.

  1. Set the height constraint's constant to tableView.contentSize.height after reloading the data.

    self.tableViewHeight.constant = self.tableView.contentSize.height
    

Set tableView size equal with contentSize.

override func viewDidLayoutSubviews() {
    tableView.frame.size = tableView.contentSize
}

You need to set the UITableView scroll to false,

tableview.scrollEnabled = false;

tableview.frame = CGRectMake(tableview.frame.origin.x, tableview.frame.origin.y, tableview.frame.size.width, tableview.frame.size.height+(resultArray.count*HEIGHT_OF_CELL));

And then you can add the UITableView on UIScrollView by setting it's content size as UITableView height.


Basically, tableview's content size is all you need.

  1. Set tableview scrolling to false because we don't want scolling behaviour.

  2. Get the reference of tableview's height constraint.

  3. set tableViewHeightConstraint.constant = tableView.contentSize.height

Make sure, tableview's top, bottom, leading and trailing constraint and are in-place.


  1. Make your tableview frame cover all lines possible -> no scroll for table view
  2. Set contentSize of your scrollview = size of your tableViews.