15

Our UITableView exists on a ViewController (and not a TableViewController). We populate it at run time with two rows. When we set a height constraint the table appears fine. When we don't set the height constraint the table completely does not appear. How can you set the UITableView to dynamically set its height?

I've seen threads about dynamically setting the row height (which does work well) but not about the actual table height. There are a few threads on this specific point but none seem to work for me.


Update on solution: As Robotic Cat suggested we set up a height constraint on the UITableView in the Storyboard (we had that already) and then did Ctrl drag to create an IBOutlet in our controller. From there we executed the following code in viewWillAppear per Vignesh's answer:

tableViewHeightConstraint.constant = tableView.contentSize.height

Note before executing the above code we are calling reloadData() in the same method to populate our table.

Marcus Leon
  • 55,199
  • 118
  • 297
  • 429
  • 2
    Constraints are just another object you can reference via an `IBOutlet`. Set your view controller constraints in the Storyboard and then create an outlet to the `UITableView` height constraint in your `UIViewController` subclass (or the constraint that controls the height of the `UITableView` like the distance to the bottom of the screen). Then set the appropriate value for the height (like a fixed value or a proportion of the screen). – Robotic Cat Mar 14 '17 at 19:42

2 Answers2

23

We don't have automatic dimension for UITableView, they only affect the UITableViewCell height. If you want to set the table view height dynamically, I think the best way is to set the table view height constraint equal to its content size after the table view done loading. I mean something like,

tableViewHeightConstraint.constant = tableView.contentSize.height
Vignesh
  • 10,205
  • 2
  • 35
  • 73
  • Keep in mind that this is ok if contentSize.height is less than view height. If you know that you will always have only two cells and they will always be of equal height, tableView might not be needed, you can have a simple UIView. – silentBob Mar 14 '17 at 20:16
  • 1
    @silentBob. I agree Thanks, but i thought it is obvious, and marcus wouldn't have asked the question if the table view size is ever to cross the view height. For completion, this could be easily fixed by Wrapping in a MIN(contentSize, viewSize). – Vignesh Mar 14 '17 at 20:22
  • Thanks. In the future we might have three or four rows, etc. I will use the min function if we do. We went with a table view and not a simple UIView as we wanted each cell to have the exact same layout. I didn't know how to "clone" the layout to different rows without a table (if that makes sense..). – Marcus Leon Mar 14 '17 at 20:58
1

You can change tableView frame in multiple ways, with animations etc.

tableView.frame = CGRect(x: leftTableBorder, y: topTableBorder, width: tableWidth, height: tableHeight)

Or you can change tableView related constraints and then update constraints with layoutIfNeeded, also animated if you wish.

For example if you have a height constraint property:

tableViewHeightConstraint.constant = desiredHeight

layoutIfNeeded()

In any case, constraints are needed, so autolayout knows how to render the table.

silentBob
  • 170
  • 9