Andre
Andre

Reputation: 7658

iOS swift: autoresize collection view nested inside a table view cell

Evening,

I have the following design:

When a collection view is empty I would like the row, or the collection view to autoresize to 0.

Currently I got this:

enter image description here

So I want the orange box autoresizing to height = 0 when there aren't blue box.

I know that I should use a constraint of collectionView Height and set to 0 when is empty.

But i have the outlet in the table cell and the collection delegate into the view controller. And I don't know how to accomplish this.

Any tips? With some code example please :P

Upvotes: 1

Views: 1194

Answers (2)

Alen Alexander
Alen Alexander

Reputation: 735

Try to use

func tableView(_ tableView: UITableView, 
         heightForRowAt indexPath: IndexPath) -> CGFloat

function. In this function, for the corresponding row, return 0.

Upvotes: 2

Vladyslav Zavalykhatko
Vladyslav Zavalykhatko

Reputation: 17414

I don't think it's possible to resize collectionView depending on its content automatically.

What you can actually do is add constraint of height to your CollectionView and set it to 0 if it's empty.

Then, your cell will have intrinsic height, if you connect CollectionView bottom and top to it accordingly (which you possibly already done)

The last step will be to set tableView.rowHeight to UITableViewAutomaticDimension. Probably it will work :)

Upvotes: 3

Related Questions