Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acrookston/b270618d0e11d4ef5f78 to your computer and use it in GitHub Desktop.
Save acrookston/b270618d0e11d4ef5f78 to your computer and use it in GitHub Desktop.
Strechy UICollectionView section header
//
// StretchCollectionViewFlowLayout.swift
//
// Created by Andrew C on 5/8/15.
//
import UIKit
class StretchCollectionViewFlowLayout: UICollectionViewFlowLayout {
override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool {
return true
}
override func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]? {
let insets = collectionView?.contentInset
let offset = collectionView?.contentOffset
let minY = -insets!.top
let attributes = super.layoutAttributesForElementsInRect(rect)
if offset!.y < minY {
let size = headerReferenceSize
let deltaY = CGFloat(fabsf(Float(offset!.y - minY)))
for attr in attributes as! [UICollectionViewLayoutAttributes] {
if attr.representedElementKind != nil && attr.representedElementKind == UICollectionElementKindSectionHeader {
var rect = attr.frame
rect.size.height = max(minY, size.height + deltaY)
rect.origin.y = rect.origin.y - deltaY
attr.frame = rect
break
}
}
}
return attributes
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment