@@ -122,6 +122,7 @@ - (void)setupDefaults {
122122 self.totalContentSize = CGSizeZero;
123123 self.sectionSpacing = 10 .f ;
124124 self.contentInsets = UIEdgeInsetsMake (5 ., 5 ., 5 ., 5 .);
125+ self.shouldPinSectionHeadersToTop = YES ;
125126}
126127
127128#pragma mark - Layout calculation -
@@ -568,22 +569,24 @@ - (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind
568569 if (sectionData.headerAttributes ) {
569570 attributes = [sectionData.headerAttributes copy ];
570571
571- NSInteger section = indexPath.section ;
572- NSInteger numberOfItemsInSection = [self .collectionView numberOfItemsInSection: section];
573-
574- NSIndexPath *firstCellIndexPath = [NSIndexPath indexPathForItem: 0 inSection: section];
575- NSIndexPath *lastCellIndexPath = [NSIndexPath indexPathForItem: MAX (0 , (numberOfItemsInSection - 1 )) inSection: section];
576-
577- UICollectionViewLayoutAttributes *firstCellAttributes = [self layoutAttributesForItemAtIndexPath: firstCellIndexPath];
578- UICollectionViewLayoutAttributes *lastCellAttributes = [self layoutAttributesForItemAtIndexPath: lastCellIndexPath];
579-
580- if (firstCellAttributes && lastCellAttributes) {
581- CGFloat headerHeight = attributes.frame .size .height ;
582- CGFloat minY = CGRectGetMinY (sectionData.sectionRect );
583- CGFloat maxY = CGRectGetMaxY (sectionData.sectionRect ) - headerHeight;
584- CGFloat yOffset = MIN (MAX (self.collectionView .contentOffset .y + self.collectionView .contentInset .top , minY), maxY);
585- attributes.frame = CGRectMake (0 .0f , yOffset, self.collectionViewContentSize .width , headerHeight);
586- attributes.zIndex = NSIntegerMax;
572+ if (self.shouldPinSectionHeadersToTop ) {
573+ NSInteger section = indexPath.section ;
574+ NSInteger numberOfItemsInSection = [self .collectionView numberOfItemsInSection: section];
575+
576+ NSIndexPath *firstCellIndexPath = [NSIndexPath indexPathForItem: 0 inSection: section];
577+ NSIndexPath *lastCellIndexPath = [NSIndexPath indexPathForItem: MAX (0 , (numberOfItemsInSection - 1 )) inSection: section];
578+
579+ UICollectionViewLayoutAttributes *firstCellAttributes = [self layoutAttributesForItemAtIndexPath: firstCellIndexPath];
580+ UICollectionViewLayoutAttributes *lastCellAttributes = [self layoutAttributesForItemAtIndexPath: lastCellIndexPath];
581+
582+ if (firstCellAttributes && lastCellAttributes) {
583+ CGFloat headerHeight = attributes.frame .size .height ;
584+ CGFloat minY = CGRectGetMinY (sectionData.sectionRect );
585+ CGFloat maxY = CGRectGetMaxY (sectionData.sectionRect ) - headerHeight;
586+ CGFloat yOffset = MIN (MAX (self.collectionView .contentOffset .y + self.collectionView .contentInset .top , minY), maxY);
587+ attributes.frame = CGRectMake (0 .0f , yOffset, self.collectionViewContentSize .width , headerHeight);
588+ attributes.zIndex = NSIntegerMax;
589+ }
587590 }
588591 }
589592 }
@@ -673,6 +676,12 @@ - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state object
673676 return [self .sectionsData countByEnumeratingWithState: state objects: buffer count: len];
674677}
675678
679+ #pragma mark - Layout information helpers -
680+
681+ - (CGRect)contentFrameForSection : (NSInteger )section {
682+ return (section < self.sectionsData .count ) ? [self .sectionsData[section] sectionRect ] : CGRectZero;
683+ }
684+
676685@end
677686
678687@implementation FSQCollectionViewAlignedLayoutSectionAttributes
0 commit comments