title: UICollectionViewCell - animate cell height change when its content changes
body: <p>I have a really simple <code>UICollectionView</code>, using iOS 13's <code>UICollectionViewCompositionalLayout</code>. The cells have a vertical <code>UIStackView</code>, and when you press the "toggle details" button, I simple change the <code>isHidden</code> value of the <code>detailsView</code>. When the cells then inform the view controller that it needs to execute <code>collectionView.collectionViewLayout.invalidateLayout()</code>, everything basically works: the details view is toggled on and off. But it doesn't animate this, so it looks a bit jarring.</p>
  
  <p><a href="https://i.stack.imgur.com/KloFr.png" rel="noreferrer"><img src="https://i.stack.imgur.com/KloFr.png" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/RntHx.png" rel="noreferrer"><img src="https://i.stack.imgur.com/RntHx.png" alt="enter image description here"></a></p>
  
  <pre class="lang-swift prettyprint-override"><code>class ViewController: UIViewController {
    @IBOutlet private var collectionView: UICollectionView!
  
    override func viewDidLoad() {
      super.viewDidLoad()
      collectionView.collectionViewLayout = createLayout()
    }
  
    private func createLayout() -&gt; UICollectionViewLayout {
      let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .estimated(200))
      let item = NSCollectionLayoutItem(layoutSize: itemSize)
  
      let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .estimated(200))
      let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])
  
      let section = NSCollectionLayoutSection(group: group)
      section.contentInsets = .init(top: 16, leading: 16, bottom: 16, trailing: 16)
      section.interGroupSpacing = 16
  
      let layout = UICollectionViewCompositionalLayout(section: section)
      return layout
    }
  }
  
  extension ViewController: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
      return 10
    }
  
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! Cell
      cell.resize = { [weak self] in
        UIView.animate(withDuration: 2) {
          self?.collectionView.collectionViewLayout.invalidateLayout()
        }
      }
      return cell
    }
  }
  
  class Cell: UICollectionViewCell {
    @IBOutlet private var detailsView: UIView!
  
    var resize: (() -&gt; Void)?
  
    override func awakeFromNib() {
      super.awakeFromNib()
      contentView.layer.cornerRadius = 10
    }
  
    @IBAction private func toggleDetails() {
      detailsView.isHidden.toggle()
      resize?()
    }
  }
  </code></pre>
  
  <p>Repro project: <a href="https://github.com/kevinrenskers/CellHeight" rel="noreferrer">https://github.com/kevinrenskers/CellHeight</a>.</p>
  
  <p>How can I animate the toggling of this details view, so that the height change animates? And can that be used in combination with the <code>UIStackView</code> and showing/hiding its children views? Please note that the details view doesn't have a fixed height in the real world app, so I can't just switch the cell height between two fixed values.</p>