Created
May 8, 2015 21:31
-
-
Save acrookston/b270618d0e11d4ef5f78 to your computer and use it in GitHub Desktop.
Strechy UICollectionView section header
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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