Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set UITableView's height to the height of its content with Auto Layout

I have a View which has two labels and a Table View inside it. I want label 1 to always stay above my Table View and label 2, to be below the Table View. The problem is that the Table View needs to auto-size meaning either increase in height or decrease.

Right now I have a constraint saying the Table View's height is always equal to 85 and a @IBOutlet to the height constraint where i'm able to change the constant.

I'm guessing I need to change the constant to the height of all the cells, but i'm not sure how.

Menu constraints

like image 287
Dumpen Avatar asked Oct 21 '15 20:10

Dumpen


People also ask

How do I change cell height in Swift?

To change the height of tableView cell in ios dynamically, i.e resizing the cell according to the content available, we'll need to make use of automatic dimension property. We'll see this with the help of an sample project.

How do I change the dynamic height of a table in Swift?

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

How do you give space between tableView cells in Swift?

The basic idea is to create a new section (rather than a new row) for each array item. The sections can then be spaced using the section header height. Set up your project as described in UITableView example for Swift. (That is, add a UITableView and hook up the tableView outlet to the View Controller).


2 Answers

You have to override updateViewConstraints() in your UIViewController and set the height constraint's constant to tableView.contentSize.height:

override func updateViewConstraints() {     tableHeightConstraint.constant = tableView.contentSize.height     super.updateViewConstraints() } 

Then you have to make sure that Label2 has a top constraint that is greaterThanOrEqual to the table view's bottom. And you also have to change the table view's height constraint's priority from Required to High to avoid conflicting constraints when the table view's contentHeight is larger than the available height.

like image 169
joern Avatar answered Oct 19 '22 08:10

joern


There is another way to do that. You can add an observer on table view contentSize variable, and self size when change in table view content.

@IBOutlet weak var tableView: UITableView! @IBOutlet weak var tableHeightConstraint: NSLayoutConstraint!  override func viewDidLoad() {     super.viewDidLoad()     self.tableView.addObserver(self, forKeyPath: "contentSize", options: NSKeyValueObservingOptions.new, context: nil) } override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {     tableView.layer.removeAllAnimations()     tableHeightConstraint.constant = tableView.contentSize.height     UIView.animate(withDuration: 0.5) {         self.updateViewConstraints()     }  } 
like image 43
Hamed Nova Avatar answered Oct 19 '22 08:10

Hamed Nova