First, a warning:
What you are trying to achieve is not standard iOS behavior. You should first consider another approach, like creating a single, grouped table view with several sections. You can implement custom views within your table view as headers or section footers.
Now, if you really want to go with your original approach ...
... for some important reason, you should know that by default there is no internal content to represent the table. Thus, you need to say that the table should look so high, because otherwise it will only decrease to zero height.
You can achieve this by either subclassing the UITableView or overriding its intrinsicContentSize() , as Rob suggests in this answer to a similar question.
Or you add a height constraint to each of the table views and dynamically set your constants in the code. Quick example:
- Add both table views to the vertical stack view in Interface Builder.
- Give both tabular views the leading and final constraints to bind their left and right edges to the stack view.
Create output points for the presentation of tables in the appropriate view controller:
@IBOutlet weak var tableView1: UITableView! @IBOutlet weak var tableView2: UITableView! @IBOutlet weak var tableView1HeightConstraint: NSLayoutConstraint! @IBOutlet weak var tableView2HeightConstraint: NSLayoutConstraint!
Cancel the updateViewConstraints() method of this controller:
override func updateViewConstraints() { super.updateViewConstraints() tableView1HeightConstraint.constant = tableView1.contentSize.height tableView2HeightConstraint.constant = tableView2.contentSize.height }
Now, when the contents of any of your table views change (for example, when you add or delete rows or change the contents of a cell), you need to tell the controller of your view that it needs to update its restrictions. Let's say you have a button that adds a cell to tableView1 every time you click it. You can implement its action as follows:
@IBAction func buttonTappen(sender: AnyObject) {
TL; dr:
A UITableView not intended to be used without scrolling, and therefore you always need to explicitly set its height when its contents change - it may use restrictions or override the internal size of the table contents.
source share