2

I have a viewController and I want to have two 1 tableView and 1 childViewController inside it.

  • tableView is non-scrollable with dynamic cell heights. (I'm using a tableView so I can collapse rows)
  • childVC is a scrollable tableView with dynamic cell heights.

my constraints are as:

NSLayoutConstraint.activate([
   tableView.topAnchor.constraint(equalTo: view.topAnchor),
   tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
   tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
   tableView.bottomAnchor.constraint(equalTo: artistVC.view.topAnchor),

])

NSLayoutConstraint.activate([
   artistVC.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
   artistVC.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
   artistVC.view.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])

When I setup this way, only the childVC shows on the screen. I don’t see the tableView. I want to allow the tableView to expand as much as it needs and then let the childVC be scrollable to the bottom.

  • for the tableView I get: Height and scrollable content size are ambiguous
  • for the childVC’s view I get: Height and vertical positions are ambiguous

However I can't set the height myself as I don't know what the height of the tableViewCell is..

Any suggestions? I've tried changing the content hugging and content Compression Resistance but I didn't find any luck there.

mfaani
  • 33,269
  • 19
  • 164
  • 293

2 Answers2

4

I suggest subclassing UITableView like this:

class AutomaticHeightTableView: UITableView {

  override var intrinsicContentSize: CGSize {
    return contentSize
  }

  override func reloadData() {
    super.reloadData()
    invalidateIntrinsicContentSize()
  }
}

Then in Interface Builder set AutomaticHeightTableView as the class to your table view and the table will try to size itself to fit the content. It will follow any other constraints you placed so make sure the constraints allow it to grow freely.

EmilioPelaez
  • 18,758
  • 6
  • 46
  • 50
  • 1
    Just a clarification for other readers: `intrinsicContentSize` is a property of `UIView` which by default doesn’t have any intrinsic size it would just be `(0,0)`. So this would give `contentSize` as its new value. – mfaani Nov 29 '17 at 16:52
  • 2
    Also about `invalidateIntrinsicContentSize` : If your tableView needs to reload, then make sure you call `invalidateIntrinsicContentSize` when your table view reloads data so it *re-queries* for the **new** content size – mfaani Nov 29 '17 at 16:53
2

Table views and other scrollable views have no intrinsicContentSize. For example, your constraints would be fine if the participating views were say a UIImageView and a UILabel both of which can be sized by their content, but because your views are a UITableView and a UIView (neither can automatically size themselves, though you have enough constraints on the UIView to not be ambiguous) you'll need to do the sizing yourself.

To get the behavior you desire, you will need to either subclass UITableView and override intrinsicContentSize or you'll need to set a height constraint. Either way, you'll need to calculate the correct height yourself. Content hugging and compression resistance allow auto layout to adjudicate between competing intrinsicContentSizes and is thus why they're not helping in this instance.

For example:

final class SizingTableView: UITableView {
    override var intrinsicContentSize: CGSize {
        return CGSize(width: bounds.width, height: <# Some appropriate height #>)
    }
}

Then in your storyboard or xib change the class of your table view to SizingTableView and you can set a design-time placeholder for the intrinsic content size of your table view in the size inspect to resolve any warning our errors.

beyowulf
  • 15,101
  • 2
  • 34
  • 40
  • 1
    Overriding `intrinsicContentSize` and simply returning `contentSize` should do the trick. No need to calculate anything. – EmilioPelaez Nov 28 '17 at 18:46
  • @EmilioPelaez umm do you mean add `override var preferredContentSize: CGSize { get { self.tableView.layoutIfNeeded() return self.tableView.contentSize } set {} }` to my viewController? I copied it from [here](https://stackoverflow.com/a/17939938/5175709). And I don't need to do anything else?! – mfaani Nov 28 '17 at 18:56
  • 1
    `preferredContentSize` is a property of `UIViewController` `intrinsicContentSize` is a property of `UIView`. Autolayout uses `intrinsicContentSize` and it is what you will need to return to resolve ambiguity. You might be able to do as others suggest and return `contentSize` which is a property of `UIScrollView`. – beyowulf Nov 28 '17 at 19:08
  • @beyowulf thank you for elaborating their distinctions. So long story short, I have to calculate the height. That's very un-intrinsic. Still I don't understand **how** I should calculate it, I mean if table turns out to be 300points and I specify it to be 250 points what's going to happen then? Nor I understand from **where** or **when** I should calculate the height from – mfaani Nov 28 '17 at 19:40
  • I would start with emilio's suggestion, which is to return the table view's `contentSize`. If that doesn't work, you can ask the delegate for the number of sections and the number of rows in each section and the height for each row with that information you can calculate the total height of the table view. It will be more more complicated if you are using `UITableViewAutomaticDimensions` – beyowulf Nov 28 '17 at 19:53
  • @EmilioPelaez so I followed what you said and did: `final class SizingTableView: UITableView { override var intrinsicContentSize: CGSize { return contentSize } }` and interesting now I only see the tableView. I no longer see my childVC. Do I also need to do something for setting the childVC's view intrinsic size as well? – mfaani Nov 28 '17 at 20:00
  • Is the `contentSize` as large as its superview? Your child view controller is pinned on all four sides so should not have ambiguity. – beyowulf Nov 28 '17 at 20:36
  • 1
    @Honey `Debug -> View Debugging -> Capture View Hierarchy` will capture your hierarchy so you can explore it and see the size of your objects. I would also recommend overriding this method on your class `override func reloadData() { super.reloadData / invalidateIntrinsicContentSize() }` – EmilioPelaez Nov 29 '17 at 00:48
  • 1
    :beyowulf "Is the contentSize as large as its superview?" great question. Facepalm! I have a real project and a dummy project for testing this issue. In my real project the tableView is smaller that its superView and that's why it isn't scrolling, but in my dummy project it was bigger than superView. once I removed a few elements from the dataSource array it started to work all good. The first comment of @EmilioPelaez was enough. – mfaani Nov 29 '17 at 15:11