UICollectionView with autosizing cells incorrectly positions supplementary view : <p>I am using a UICollectionView with a Flow Layout and trying to get the collectionView to size the cells appropriately according to AutoLayout constraints.</p>

<p>While the cells work as intended, I am running in to issues with the layout of any supplementary views that I add to the CollectionView.</p>

<p>Specifically, the supplementaryView will be in the wrong position (i.e., the y origin is incorrect) on initial layout, before 'correcting' itself after I scroll.</p>

<p><a href="https://i.stack.imgur.com/zkS1T.png" rel="noreferrer"><img src="https://i.stack.imgur.com/zkS1T.png" alt="enter image description here"></a></p>

<p>For reference, here is how I am configuring my cell sizing:</p>

<p><strong>1. Set the collectionViewLayout's estimated item size</strong></p>

<pre><code>let collectionView: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    layout.estimatedItemSize = CGSizeMake(375, 50.0)
    layout.minimumInteritemSpacing = 0.0
    layout.minimumLineSpacing = 0.0
    let view = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
    view.backgroundColor = UIColor.whiteColor()
    view.alwaysBounceVertical = true
    return view
}()
</code></pre>

<p><strong>2. Use subclasses of AutoLayoutCollectionViewCell</strong></p>

<pre><code>class AutoLayoutCollectionViewCell: UICollectionViewCell {
    override func preferredLayoutAttributesFittingAttributes(layoutAttributes: UICollectionViewLayoutAttributes) -&gt; UICollectionViewLayoutAttributes {
        layoutIfNeeded()
        layoutAttributes.bounds.size.height = systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height
        return layoutAttributes
    }
}
</code></pre>

<p>Note that at this point, everything works as intended.</p>

<p>The next step is where we fail.</p>

<p><strong>3. Provide a reference size for a header</strong></p>

<pre><code>func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -&gt; CGSize {
return CGSizeMake(CGRectGetWidth(collectionView.frame), 30.0) 
</code></pre>

<p>}</p>

<p>My question is: Why does this happen? How can I get this to correct? How am I supposed to handle supplementary views within a collectionView that self-sizes its cells??</p>
