Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.