title: UICollectionView not anchoring to the top of my UIView
body: <p>So I'm trying to anchor a UICollectionView onto the top of my view. I'm using anchors to help me. This is the code I have so far:</p>
  
  <pre><code>class MenuBar : UIView, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
  
      let cellID : String = "menuCell"
  
  
      lazy var collectionView : UICollectionView = {
          let cv : UICollectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
          cv.backgroundColor = red
          cv.translatesAutoresizingMaskIntoConstraints = false
          //cv.isScrollEnabled = false
          cv.delegate = self
          cv.dataSource = self
          return cv
      }()
  
      override init(frame: CGRect) {
          super.init(frame: frame)
          setupCollectionView()
  
      }
  
      func setupCollectionView() {
          addSubview(collectionView)
          collectionView.register(MenuBarCell.self, forCellWithReuseIdentifier: cellID)
  
          // setup menuBar constraints
          collectionView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
          collectionView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
          collectionView.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
          collectionView.heightAnchor.constraint(equalTo: self.heightAnchor).isActive = true
      }
  
      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
          let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath)
          cell.backgroundColor = .white
          return cell
      }
  
      func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
          return 4
      }
  }
  </code></pre>
  
  <p>I'm adding the MenuBar into the ViewController using these two pieces of code:</p>
  
  <pre><code>let menuBar : MenuBar = {
          let mb = MenuBar()
          mb.translatesAutoresizingMaskIntoConstraints = false
          return mb
      }()
  
  func setupMenuBar() {
          view.addSubview(menuBar)
  
          menuBar.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
          menuBar.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
          menuBar.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
          menuBar.heightAnchor.constraint(equalToConstant: 200).isActive = true
      }
  </code></pre>
  
  <p>In <code>viewDidLoad</code>, I call <code>setupMenuBar</code>.</p>
  
  <p>But the UICollectionView seems to always be offset by a set amount of pixels from the top of the view, but I don't see any reason for this to happen. This is what I see:</p>
  
  <p><a href="https://i.stack.imgur.com/yXRHH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yXRHH.png" alt="enter image description here"></a></p>
  
  <p>Basically what I want is to have the leftmost cell be in the top left of the view.</p>
  
  <p>Can anyone help me figure this out? Thank you in advance!</p>