ymotov
ymotov

Reputation: 1489

UICollectionView as a subview doesn't update constraints

I'm having an autolayout puzzle. Here's the description:

I have a UITableViewCell that has a "container view" which is a basic UIView and then UICollectionView is a subview of this container view.

UITableViewCell -> UIView -> UICollectionView

It's a bit simplified from the end goal which includes a little more views under the tableViewCell, but for the sake of the question it should be good enough.

Both UIView and UICollectionView have constraints in the following format:

H:|[view]| and V:|[view]| (e.g. take full size of the tableViewCell).

For some reasons the constraints are not being updated after UICollectionView lays out it's cells.

Here's what works:

  1. If I replace UICollectionView with a UILabel, then it works fine (e.g. tableViewCell is properly dynamically sized depending on the label text).

  2. If I remove the intermediate UIView (the container view) from the puzzle then again it all works fine (e.g. tableViewCell is properly dynamically sized depending on number of cells in UICollectionView).

What doesn't work is when I have UICollectionView added to the intermediate view like this: UITableViewCell -> UIView -> UICollectionView

Any ideas how to solve this?

Upvotes: 0

Views: 532

Answers (1)

Doro
Doro

Reputation: 2413

Apple does not recommend use as subviews other scrollable objects inside uitableview.

To achieve your goal:

  1. Add equalWidth constaint for your UICollectionView (to uitableviewcell)
  2. Add equalHeight constaint for your UICollectionView (to uitableviewcell)

Upvotes: 1

Related Questions