title: Nest a UICollectionView inside a UIVIew programmatically
body: <p>I am attempting to get a UICollectionView to be inside a UIView as the framework I'm using requires a UIView to be returned. I have looked at this question: <a href="https://stackoverflow.com/questions/21514617/how-do-i-add-a-uicollectionview-to-a-uiview-as-a-subview">How do I add a UICollectionView to a UIView as a subview?</a> and <a href="https://stackoverflow.com/questions/44355454/adding-uicollectionview-inside-uiview-without-storyboards">Adding UICollectionView inside UIView without Storyboards</a> but not sure how to get it to work.</p>
  
  <p>I have a attempted it like this:</p>
  
  <pre><code>class TopView : UIView, UICollectionViewDataSource, UICollectionViewDelegate {
  
      override init(frame: CGRect) {
          super.init(frame: frame)
          self.backgroundColor = .red
  
          addSubview(collectionView)
      }
  
      required init?(coder aDecoder: NSCoder) {
          super.init(coder: aDecoder)
      }
  
      lazy var collectionView : UICollectionView = {
          let layout = UICollectionViewFlowLayout()
          let cv = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
          cv.translatesAutoresizingMaskIntoConstraints = false
          cv.delegate = self
          cv.dataSource = self
          cv.register(HeaderCell.self, forCellWithReuseIdentifier: "HeaderCell")
          cv.backgroundColor = .yellow
  
          return cv
      }()
  
      func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
          return 1
      }
  
      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
  
          let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HeaderCell", for: indexPath) as! HeaderCell
  
          return cell
  
      }
  
      func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -&gt; CGSize {
          return CGSize(width: self.collectionView.frame.width, height: 200)
      }
  
      func collectionView(_ collectionView: UICollectionView, numberOfSections section: Int) -&gt; Int {
          return 1
      }
  
  }
  </code></pre>
  
  <p>But I'm getting a blank screen.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>This is how I add the TopView to a UIViewController:</p>
  
  <pre><code>class MainViewController: UIViewController {
  
      var mainView = TopView()
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          view.addSubview(mainView)
      }
  }
  </code></pre>
  
  <p>I get just a black screen.</p>