title: How to redraw a dynamically sized UIView within a collection cell
body: <p>I have a <code>UICollectionView</code>, and inside each <code>UICollectionViewCell</code> is a <code>UIView</code> that I am sizing dynamically based on the content...</p>
  
  <p>From the data source I am setting a model on the cell:</p>
  
  <pre><code>extension CategoryViewController: UICollectionViewDataSource {
      // Show quiz thumbnail and name
      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
          let cell: QuizViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! QuizViewCell
  
          cell.quiz = self.quizzes[indexPath.row]
  
          return cell
      }
  }
  </code></pre>
  
  <p><code>cell.quiz</code> is a computed property. When it is set, I calculate the width of a "progress bar" based on the difficulty. For simplicity I am just setting it to 100 here.</p>
  
  <pre><code>class QuizViewCell: UICollectionViewCell {
      @IBOutlet weak var difficultyBar: UIView!
  
      var quiz: Quiz? {
          didSet {
              configure(quiz)
          }
      }
  
      private func configure(_ quiz: Quiz?) {
          if let quiz = quiz {
              difficultyBar.frame = CGRect(x: 0, y: 0, width: 100, height: 6)
  
              // Attempt to redraw bar, but doesn't work
              setNeedsLayout()
              layoutIfNeeded()
          }
      }
  }
  </code></pre>
  
  <p>I know that if it weren't for the collection, I could just call <code>view.layoutIfNeeded()</code> on the controller to force it to redraw the bar now that its width has been set. But I can't figure out where or how to force it to redraw a view that is within a cell within a collection view.</p>