simplexity
simplexity

Reputation: 1557

Dynamically-sized TableView creating AutoLayout issues

I have a ScrollView that takes up the entire device screen.

Inside of this ScrollView there is a View that hold 3 Labels, underneath a TableView, and then at last underneath, a Label.

Layout in Story Baord

Now, the TableView has an unknown amount of items being injected into it and I do not want it to scroll, so I have attached its height as a NSLayoutContraint that is dynamically sized and reloaded once the items have been parsed.

This is how the table is being resized after the data comes in:

@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var tableHeight: NSLayoutConstraint!

var array = [String]()

let import = self.items!["people"] as? String

array = import!.componentsSeparatedByString(",")

let height = CGFloat(self.array.count * 50)

self.tableHeight.constant = height

self.tableView.reloadData()

That is all well and dandy, but the problem comes with the AutoLayout and I keep getting errors. I have tried the following things:

  1. Setting the inner View layout to be pinned against the ScrollView. First, Xcode is saying "Scrollable content size ambiguous for ScrollView" and when running it, there are no console errors but the View expands horizontally past the ScrollView viewport and the Label underneath the dynamic content shows.

Try number 1

Number 2 prescribed by: UIScrollView Scrollable Content Size Ambiguity

  1. Setting the inner View to be centered horizontally and centered vertically with the ScrollView. This removed the ambiguous content size error (Yay!), and corrected the View from running past the ScrollView viewport (Yay!), but the Label underneath the dynamically-sized TableView is not hidden (assuming its under the TableView) BUT I am now understandably getting the Unable to simultaneously satisfy constraints. error in the console.

Try number 2

Those are the two outcomes I have come to but I am now stuck. I have checked the steps about ScrollViews in the Apple Documentation and followed it exactly to be sure, but I think I am having this issue because of the dynamically-sized TableView or some AutoLayout mistake I am making.

Can anyone help? Thank you!

Upvotes: 0

Views: 106

Answers (2)

Sabby
Sabby

Reputation: 403

use these two lines code in tableview delegate method height for Row


self.tableView.estimatedRowHeight = 44.0;
return tableView.rowHeight = UITableViewAutomaticDimension;

Upvotes: 0

user5104686
user5104686

Reputation:

Have you toyed around with using stack views for this? You should be able to set the scroll view as a stack and everything else in other ones and then just overlay them. You may have to use the X, Y option because dragging will drop it into the stack view.

Upvotes: 1

Related Questions