title: How to make UICollectionView class programmatically in iOS Swift 3 without using storyboard?
body: <p>I made a class <em>GalleryCollectionViewController</em> that inherited from UICollectionView like this:  </p>
  
  <pre><code>import UIKit  
  
  class GalleryCollectionViewController: UICollectionViewController {
  
  var dataSourceArr:Array&lt;UIImage&gt;!
      override convenience init(collectionViewLayout layout: UICollectionViewLayout) {
          self.init()
          collectionView?.collectionViewLayout = layout
          collectionView!.register(GalleryCollectionViewCell.self, forCellWithReuseIdentifier: "cell")
      }
  
      override func viewDidLoad() {
          super.viewDidLoad()
      }
  
     // MARK: UICollectionViewDataSource
  
      override func numberOfSections(in collectionView: UICollectionView) -&gt; Int {
          // #warning Incomplete implementation, return the number of sections
          return 1
      }
  
      override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
          // #warning Incomplete implementation, return the number of items
          if dataSourceArr.count != 0 {
              return dataSourceArr.count
          }
          return 0
      }
  
      override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
  
          let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! GalleryCollectionViewCell
  
          cell.imageView.image = dataSourceArr[indexPath.row]
  
          return cell
      }  
  </code></pre>
  
  <p><em>GalleryCollectionViewCell</em> has defined.  </p>
  
  <p>And in root controller set this in <em>viewDidLoad</em>  :</p>
  
  <pre><code>let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
  layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10)
  layout.itemSize = CGSize(width: 90, height: 120)
  let galleryColVC = GalleryCollectionViewController(collectionViewLayout: layout)
  galleryColVC.dataSourceArr = photoLibraryImagesArr
  self.present(galleryColVC, animated: true, completion: nil)
  </code></pre>
  
  <p>And but get this error in UICollectionView :  </p>
  
  <blockquote>
    <p>*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be
    initialized with a non-nil layout parameter'  </p>
  </blockquote>
  
  <p>Please help to fix this.</p>