title: Programmatically create UICollectionView with custom headers
body: <p>I'm making an iOS app in swift, and I'm trying to make a collectionView programmatically.
  I want to use my own subclass of UICollectionReusableView as a header for the CollectionView, because I need some buttons and a stretchable image in the header.</p>
  
  <p>SupView is the UICollectionReusableView.</p>
  
  <pre><code>override func viewDidLoad() {
      super.viewDidLoad()
  
  
      let layout = UICollectionViewFlowLayout()
      layout.headerReferenceSize = CGSizeMake(self.view.frame.width, 200)
  
      someView = SupView(frame: CGRectMake(0, 0, view.frame.width, 200))
  
      collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
      collectionView.delegate = self
      collectionView.dataSource = self
  
      collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
      collectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headerCell")  // UICollectionReusableView
      self.view.addSubview(collectionView)
  }
  </code></pre>
  
  <p>I'm trying to insert the Supplementary View in <code>viewForSupplementaryElementOfKind</code>, like this but I'm getting an error when trying to create the header:</p>
  
  <pre><code>func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -&gt; UICollectionReusableView {
      var reusableView : UICollectionReusableView? = nil
  
      // Create header
      if (kind == UICollectionElementKindSectionHeader) {
          // Create Header
          let headerView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "headerCell", forIndexPath: indexPath) as! SupView
          headerView.frame = CGRectMake(0, 0, view.frame.width, 200)
  
          reusableView = headerView
      }
      return reusableView!
  }
  </code></pre>
  
  <p>The error is in <code>let headerView = ...</code> and says: "signal SIGABRT"</p>
  
  <p><strong>How should I initialize the headerview, so the I can input to my flowlayout?</strong></p>
  
  <p>maybe somewith with </p>
  
  <pre><code>collectionView.registerClass(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headerCell")
  </code></pre>
  
  <p>but if I try to register the SupView-class it gives me error: </p>
  
  <blockquote>
    <p>.../collectionViewPlay/ViewController.swift:32:24: Cannot invoke 'registerClass' with an argument list of type '(SupView!, forSupplementaryViewOfKind: String, withReuseIdentifier: String)'</p>
  </blockquote>
  
  <p>Any Ideas?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>The implementation of the subclass was requested:</p>
  
  <pre><code>import UIKit
  
  class SupView: UICollectionReusableView {
  
      override init(frame: CGRect) {
          super.init(frame: frame)
          self.myCustomInit()
      }
  
      required init(coder aDecoder: NSCoder) {
          super.init(coder: aDecoder)!
          self.myCustomInit()
      }
  
      func myCustomInit() {
          print("hello there from SupView")
      }
  
  }
  </code></pre>