title: UICollectionView with dynamically-scaling cells
body: <p>My goal is to create a layout presented below:</p>
  <p><a href="https://i.stack.imgur.com/VVNWv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VVNWv.png" alt="The layout" /></a></p>
  <p>I know how to create these custom UICollectionViewCells, but I trouble with the layout. All of the shown cells differ in width, so there can be, for instance: four in the first row, two in the second, and the last one remaining - in the third. That's just one possible configuration, and there are many more (including one shown on an image) depending on the label's width.</p>
  <p>I'm building everything programmatically. Also I feel like using <code>UICollectionView</code> is the best choice, but I'm open to any suggestions.</p>
  <p>Thanks in advance!</p>
  <p>What I've already tried:</p>
  <pre><code>let collectionView = UICollectionView(frame: .zero, collectionViewLayout: TagLayout()
  
  override func viewDidLoad() {
      super.viewDidLoad()
      setupCollectionView()
  }
  
  private func setupCollectionView() {
      collectionView.backgroundColor = .systemGray5
      collectionView.dataSource = self
      collectionView.delegate = self
      collectionView.register(SubjectCollectionViewCell.self, forCellWithReuseIdentifier: &quot;cell&quot;)
          
      //adding a view to subview and constraining it programmatically using Stevia
  }
  
  extension ProfileVC: UICollectionViewDelegate, UICollectionViewDataSource {
      func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
          return 5
      }
      
      
      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
          guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: &quot;cell&quot;, for: indexPath) as? SubjectCollectionViewCell else { return UICollectionViewCell() }
          cell.data = SubjectTagData(emoji: &quot;&quot;, subjectName: &quot;Item I&quot;)
          
          return cell
      }
  }
  </code></pre>