0

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!

Community
  • 1
  • 1
simplexity
  • 1,527
  • 1
  • 13
  • 31

2 Answers2

1

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.

  • 1
    You are the man. Seriously, this helped **BIG TIME**. With your suggestion, I inserted a vertical `StackView` with the dynamically-sized `TableView` at the top and then a bunch of `Views` filled with `Labels`, `TableViews` etc and it worked out perfectly. – simplexity Feb 19 '16 at 18:22
  • @simplexity oh yeah I believe it, stack views will change your life! I don't know how I missed all the hype about them, but Apple actually was saying the way developers should work from now on is to do stack views first, then worry about constraints. Glad it helped! =] –  Feb 19 '16 at 20:01
0
use these two lines code in tableview delegate method height for Row


self.tableView.estimatedRowHeight = 44.0;
return tableView.rowHeight = UITableViewAutomaticDimension;
Sabby
  • 403
  • 3
  • 15
  • Thanks, I did use these in the past but they were triggering an error about zero-sized tables. The accepted answer of stack views was more in line with the solution I was looking for. For more info on these lines and their capabilities, I did write a question a while back. Check it out! [Dynamic Height Issue for UITableView Cells (Swift)](http://stackoverflow.com/a/30533619/1284269) – simplexity Feb 22 '16 at 15:25