Created
September 22, 2016 21:22
-
-
Save AntonTheDev/cd4a969aed083cd7be5c49b793032262 to your computer and use it in GitHub Desktop.
Sticky Header CollectionViewFlowLayout
This file contains hidden or 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
| class NonBlinkyCollectionViewFlowLayout: UICollectionViewFlowLayout { | |
| override init() { | |
| super.init() | |
| minimumInteritemSpacing = 0.0 | |
| minimumLineSpacing = 0.0 | |
| sectionInset = UIEdgeInsetsZero | |
| scrollDirection = .Vertical | |
| } | |
| var finalAttributes = [UICollectionViewLayoutAttributes]() | |
| required init?(coder aDecoder: NSCoder) { | |
| fatalError("init(coder:) has not been implemented") | |
| } | |
| override func initialLayoutAttributesForAppearingItemAtIndexPath(itemIndexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? { | |
| let attributes = super.initialLayoutAttributesForAppearingItemAtIndexPath(itemIndexPath) | |
| attributes?.alpha = 1.0 | |
| return attributes | |
| } | |
| override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? { | |
| let superAttributes:NSMutableArray = NSMutableArray(array: super.layoutAttributesForElementsInRect(rect)!) as NSMutableArray | |
| let contentOffset = collectionView!.contentOffset | |
| let missingSections = NSMutableIndexSet() | |
| for layoutAttributes in superAttributes { | |
| if (layoutAttributes.representedElementCategory == .Cell) && layoutAttributes.indexPath != nil { | |
| missingSections.addIndex(layoutAttributes.indexPath!.section) | |
| } | |
| } | |
| for layoutAttributes in superAttributes { | |
| if layoutAttributes.representedElementKind == UICollectionElementKindSectionHeader { | |
| if let indexPath = layoutAttributes.indexPath { | |
| missingSections.removeIndex(indexPath.section) | |
| } | |
| } | |
| } | |
| missingSections.enumerateIndexesUsingBlock { idx, stop in | |
| let indexPath = NSIndexPath(forItem: 0, inSection: idx) | |
| let layoutAttributes = self.layoutAttributesForSupplementaryViewOfKind(UICollectionElementKindSectionHeader, atIndexPath: indexPath) | |
| superAttributes.addObject(layoutAttributes!) | |
| } | |
| for la in superAttributes { | |
| let layoutAttributes = la as! UICollectionViewLayoutAttributes; | |
| if let representedElementKind = layoutAttributes.representedElementKind { | |
| if representedElementKind == UICollectionElementKindSectionHeader { | |
| let section = layoutAttributes.indexPath.section | |
| let numberOfItemsInSection = collectionView!.numberOfItemsInSection(section) | |
| let firstCellIndexPath = NSIndexPath(forItem: 0, inSection: section) | |
| let lastCellIndexPath = NSIndexPath(forItem: max(0, (numberOfItemsInSection - 1)), inSection: section) | |
| var firstCellAttributes:UICollectionViewLayoutAttributes | |
| var lastCellAttributes:UICollectionViewLayoutAttributes | |
| if (self.collectionView!.numberOfItemsInSection(section) > 0) { | |
| firstCellAttributes = self.layoutAttributesForItemAtIndexPath(firstCellIndexPath)! | |
| lastCellAttributes = self.layoutAttributesForItemAtIndexPath(lastCellIndexPath)! | |
| } else { | |
| firstCellAttributes = self.layoutAttributesForSupplementaryViewOfKind(UICollectionElementKindSectionHeader, atIndexPath: firstCellIndexPath)! | |
| if let lastAttributes = self.layoutAttributesForSupplementaryViewOfKind(UICollectionElementKindSectionFooter, atIndexPath: lastCellIndexPath) { | |
| lastCellAttributes = lastAttributes | |
| } else { | |
| lastCellAttributes = firstCellAttributes | |
| } | |
| } | |
| let headerHeight = CGRectGetHeight(layoutAttributes.frame) | |
| var origin = layoutAttributes.frame.origin | |
| origin.y = min(max(contentOffset.y, (CGRectGetMinY(firstCellAttributes.frame) - headerHeight)), (CGRectGetMaxY(lastCellAttributes.frame) - headerHeight)) | |
| ; | |
| layoutAttributes.zIndex = 1024; | |
| layoutAttributes.frame = CGRect(origin: origin, size: layoutAttributes.frame.size) | |
| } | |
| } | |
| } | |
| return NSArray(array: superAttributes) as? [UICollectionViewLayoutAttributes] | |
| } | |
| override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool { | |
| return true | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment