title: How to use a UICollectionViewController's custom initializer with UICollectionViewCompositionalLayout?
body: <p>Let's say you wanted to use a custom initializer to instantiate <code>UICollectionViewController</code>.</p>
  <p>With <code>UICollectionViewFlowLayout</code>, it's straightforward:</p>
  <pre class="lang-swift prettyprint-override"><code>init() {
      let layout = UICollectionViewFlowLayout()
      super.init(collectionViewLayout: layout)
  }
  </code></pre>
  <p>But, <code>UICollectionViewCompositionalLayout</code>'s designated initializers require a few parameters, most importantly the section parameter:</p>
  <pre class="lang-swift prettyprint-override"><code>UICollectionViewCompositionalLayout(section:, configuration:)
  // or 
  UICollectionViewCompositionalLayout(sectionProvider:, configuration:)
  </code></pre>
  <p>This means I have to create the entire layout within the custom initializer of <code>UICollectionViewController</code>. If the layout incorporates things like enum as a section identifier, that has to be in the initializer as well.  If you have to include <code>NSCollectionLayoutEnvironment</code> in your layout, it also becomes problematic.</p>
  <pre class="lang-swift prettyprint-override"><code>init() {
      let item = /..
      let group = /..
      let section = /..
      let layout = UICollectionViewCompositionalLayout(section: section)
      super.init(collectionViewLayout: layout)
  }
  </code></pre>
  <p>I'm looking at the Apple's <a href="https://developer.apple.com/documentation/uikit/views_and_controls/collection_views/implementing_modern_collection_views" rel="nofollow noreferrer">examples</a> and all of them use <code>UIViewController</code> instead of <code>UICollectionViewController</code> by default. Even though the examples don't use any custom initializers, this method does get around the aforementioned problem by allowing us to create the layout afterwards:</p>
  <pre class="lang-swift prettyprint-override"><code>var collectionView: UICollectionView! = nil
  collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: createLayout())
  </code></pre>
  <p>You can now simply use the <code>UIViewController</code>'s custom initializer.</p>
  <p>What is the best way to use <code>UICollectionViewCompositionalLayout</code> with a custom initializer?</p>