IOS: dynamically adjust the height of a UITableView in Swift

Our UITableView exists on the ViewController (and not on the TableViewController). We fill it at run time with two lines. When we set a height limit, the table looks fine. When we do not set a height limit, the table is not fully displayed. How to configure UITableView to dynamically set height?

I saw threads dynamically setting row heights (which works well), but not about the actual height of the table. There are several threads in this particular topic, but no one works for me.


Update in solution . As Robotic Cat suggested setting a height limit on UITableViewthe Storyboard (we already had this), then dragged Ctrl to create it IBOutletin our controller. From there, we executed the following code in viewWillAppearVignesh's response:

tableViewHeightConstraint.constant = tableView.contentSize.height

Please note that before executing the above code, we call reloadData()in the same method to populate our table.

+4
source share
2 answers

UITableView, UITableViewCell. , , - , , . -

tableViewHeightConstraint.constant = tableView.contentSize.height
+6

tableView , ..

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

TableView , layoutIfNeeded, , .

, :

tableViewHeightConstraint.constant = desiredHeight

layoutIfNeeded()

, , .

+1

Source: https://habr.com/ru/post/1672295/


All Articles