10

First of all this is not a question about how to automatically size the cells inside the tableview, moreover how to automatically resize the entire tableview.

So I have a scrollview which has a tableview and 2 other views as its subviews. The tableview's cells already automatically resize itself, again this question is not about the individual cells. However, the tableview does not resize at all.

What I have done: 1) Set up the tableview to have a top, bottom, leading and trailing constraint 2) Set the cells up to have auto layout enabled 3) * I do not know the cell size at build time 4) I have disabled scrolling mode on tableview

So long story short, how can I go along to get the tableview to resize itself?

Edit

The cells contain a label which can have various lines of text, so therefore the cells, which use auto layout, should then determine the height of the table view.

The following images show how the view is set up:

Quinchol Quinchol 2

As you can see the tableview is only a small part of the view and since the scrollview from the tableview is deactivated there should, and aren't, any scrolling problems.

EDIT 2

This is actually how it should end however, i am calculating this on my own and everytime I want to make a small change to the cells the whole code, which calculates the height of the cell, needs to be rewritten and it is quite difficult for me to get the height just right.

Edit 3

Until now I had a height constraint on the tableview which I calculated manually, however removing this constraint and trying to let auto layout handle the tableview height size creates the following error:

Scroll View Need constraint for: Y position or height

I can conclude therefore that the tableview does not know how to automatically calculate the height based on its cells with autolayout.

12
  • UITableView inside UIScrollView is an antipattern because it breaks the reusing of cells but if you really want to do that, add a height constraint and set it to tableView.contentSize.height programatically and after every data update. The height cannot be determined from cells.
    – Sulthan
    Jun 4, 2017 at 22:04
  • Dynamically resizing a table view based on the cell heights is unusual... Maybe if you described better what your end goal is? You say "tableview and 2 other views" ... Might you be better off using the 'other views' as Header and Footer views on the table? Then you wouldn't need the additional scroll view at all.
    – DonMag
    Jun 4, 2017 at 22:06
  • Or... maybe you shouldn't be using a Table View at all. I'm guessing you're not expecting a 500+ row table? There are other ways to show repeating variable-height views which may be a much better (easier) route than using table view cells.
    – DonMag
    Jun 4, 2017 at 22:08
  • So... is there some reason you don't want to use just a table view?
    – DonMag
    Jun 4, 2017 at 22:56
  • Well, I could probably do it without a table view but it would be quite difficult and a pain to do it. If it is possible to do it with a table view I would like to it so otherwise I will do it without Jun 5, 2017 at 0:18

2 Answers 2

14

You don't need to create a height constraint or set frame whatsoever. Create a subclass of UITableView and recalculate its intrinsicContentSize every time its contentSize changes aka new data added or removed. Here is all you needed:

class SelfSizingTableView: UITableView {
    override var contentSize: CGSize {
        didSet {
            invalidateIntrinsicContentSize()
            setNeedsLayout()
        }
    }

    override var intrinsicContentSize: CGSize {
        let height = min(.infinity, contentSize.height)
        return CGSize(width: contentSize.width, height: height)
    }
}
2
  • 1
    why setting the height = min(.infinity, contentSize.height)? When contentSize.height is always less than or equal to .infinity? Or is it?
    – eja08
    Jul 7, 2021 at 19:24
  • @eja08 Nice catch. .infinity can be replaced with a maxHeight variable so that the tbv will stop growing after a certain point. I just didn't include it here.
    – Linh Ta
    Jul 7, 2021 at 22:41
-2

You can change your UITableView's frame by using tableview.frame = CGRect(x: <some_x>, y: <some_y>, width: <some_width>, height: <some_height>)

If your UITableViewCells use auto layout then they should resize when the UITableView's frame changes.

2
  • The idea is that the cells determine the tableviews height not the other way around. Jun 4, 2017 at 22:04
  • Can you provide an example using an image or another application that uses this technique? Why would you want your cells to size your table view? This doesn't really conform to any normal design patters and I would reconsider this approach based on my understanding of your question. Jun 4, 2017 at 22:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.