title: Attempt to achieve fixed height, dynamic width (wrap content) horizontal UICollectionView's cell by using UICollectionViewCompositionalLayout
body: <p>In <a href="https://stackoverflow.com/a/51231881/72437">https://stackoverflow.com/a/51231881/72437</a>, it show how to achieve a full width, dynamic height in vertical <code>UICollectionView</code>'s cell, by using <code>UICollectionViewCompositionalLayout</code></p>
  <p>We would like to achieve the same on a horizontal <code>UICollectionView</code>, with requirements</p>
  <ol>
  <li>Fixed height 44</li>
  <li>Minimum width 44</li>
  <li>The width should be dynamically enlarge, when the content grows</li>
  </ol>
  <p>Here's how our solution looks like</p>
  <pre><code>class MenuTabsView: UIView, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
     
      lazy var collView: UICollectionView = {
          let itemSize = NSCollectionLayoutSize(
              widthDimension: NSCollectionLayoutDimension.estimated(44),
              heightDimension: NSCollectionLayoutDimension.fractionalHeight(1.0)
          )
          let item = NSCollectionLayoutItem(
              layoutSize: itemSize
          )
          item.contentInsets = NSDirectionalEdgeInsets(
              top: 0,
              leading: 0,
              bottom: 0,
              trailing: 1
          )
          let group = NSCollectionLayoutGroup.horizontal(
              layoutSize: itemSize,
              subitem: item,
              count: 1
          )
          let section = NSCollectionLayoutSection(group: group)
          
          let configuration = UICollectionViewCompositionalLayoutConfiguration()
          configuration.scrollDirection = .horizontal
          
          let layout = UICollectionViewCompositionalLayout(section: section, configuration: configuration)
          
          let cv = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: layout)
          cv.showsHorizontalScrollIndicator = false
          cv.backgroundColor = .white
          cv.delegate = self
          cv.dataSource = self
          
          return cv
      }()
  </code></pre>
  <p>We expect by using <code>widthDimension: NSCollectionLayoutDimension.estimated(44)</code>, that's the key to make the cell width grow dynamically. However, that doesn't work as expected. It looks like</p>
  <p><a href="https://i.stack.imgur.com/JCTja.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JCTja.png" alt="enter image description here" /></a></p>
  <p>May I know, how can we solve this problem by using <code>UICollectionViewCompositionalLayout</code> ? The complete workable project is located at <a href="https://github.com/yccheok/PageViewControllerWithTabs/tree/UICollectionViewCompositionalLayout" rel="nofollow noreferrer">https://github.com/yccheok/PageViewControllerWithTabs/tree/UICollectionViewCompositionalLayout</a></p>
  <hr />
  <p>p/s</p>
  <p>We want to avoid from using <code>UICollectionViewDelegateFlowLayout method collectionView(_:layout:sizeForItemAt:)</code>. As, our cell can grow complex, and it will contain other views besides <code>UILabel</code>. Having to calculate the content size manually, will make the solution inflexible and error prone.</p>