-
-
Save Tiger6688/d6f36a5ae9a1eaba609d to your computer and use it in GitHub Desktop.
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
| //Override UICollectionViewFlowLayout class | |
| @interface FixedHeaderLayout : UICollectionViewFlowLayout | |
| @end | |
| @implementation FixedHeaderLayout | |
| //Override shouldInvalidateLayoutForBoundsChange to require a layout update when we scroll | |
| - (BOOL) shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds { | |
| return YES; | |
| } | |
| //Override layoutAttributesForElementsInRect to provide layout attributes with a fixed origin for the header | |
| - (NSArray *) layoutAttributesForElementsInRect:(CGRect)rect { | |
| NSMutableArray *result = [[super layoutAttributesForElementsInRect:rect] mutableCopy]; | |
| //see if there's already a header attributes object in the results; if so, remove it | |
| NSArray *attrKinds = [result valueForKeyPath:@"representedElementKind"]; | |
| NSUInteger headerIndex = [attrKinds indexOfObject:UICollectionElementKindSectionHeader]; | |
| if (headerIndex != NSNotFound) { | |
| [result removeObjectAtIndex:headerIndex]; | |
| } | |
| CGPoint const contentOffset = self.collectionView.contentOffset; | |
| CGSize headerSize = self.headerReferenceSize; | |
| //create new layout attributes for header | |
| UICollectionViewLayoutAttributes *newHeaderAttributes = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader withIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]]; | |
| CGRect frame = CGRectMake(0, contentOffset.y, headerSize.width, headerSize.height); //offset y by the amount scrolled | |
| newHeaderAttributes.frame = frame; | |
| newHeaderAttributes.zIndex = 1024; | |
| [result addObject:newHeaderAttributes]; | |
| return result; | |
| } | |
| @end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment