Skip to content

Commit 43ca6d6

Browse files
author
Brian Dorfman
committed
Merge pull request #11 from foursquare/csmulhern-section-pinning
Add ability to have non-pinned headers. Also adds contentFrameForSection: and other documentation cleanup. Refactors and resolves conflicts in PR #9
2 parents c8643e2 + 793d58b commit 43ca6d6

File tree

2 files changed

+49
-24
lines changed

2 files changed

+49
-24
lines changed

FSQCollectionViewAlignedLayout.h

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,22 @@
8080
*/
8181
@property (nonatomic) UIEdgeInsets contentInsets;
8282

83+
/**
84+
Determines if the current section's header is pinned to the top of the collection view.
85+
86+
Defaults to YES.
87+
88+
@note This is similar to the behavior observed in UITableView's with the style UITableViewStylePlain.
89+
*/
90+
@property (nonatomic) BOOL shouldPinSectionHeadersToTop;
91+
92+
/**
93+
@param section The section of the collection view.
94+
95+
@return The frame for the section, or CGRectZero if the section is out of range.
96+
*/
97+
- (CGRect)contentFrameForSection:(NSInteger)section;
98+
8399
@end
84100

85101

@@ -106,14 +122,14 @@
106122
remainingLineSpace:(CGFloat)remainingLineSpace;
107123

108124
/**
109-
* Asks the delegate for the height of the header view in the specified section.
110-
* If you do not implement this method, or the height returned is 0, no header is added.
111-
*
112-
* @param collectionView The collection view object displaying the layout.
113-
* @param collectionViewLayout The layout object requesting the information.
114-
* @param section The index of the section whose header size is being requested.
115-
*
116-
* @return The height of the header. If you return a value of 0, no header is added.
125+
Asks the delegate for the height of the header view in the specified section.
126+
If you do not implement this method, or the height returned is 0, no header is added.
127+
128+
@param collectionView The collection view object displaying the layout.
129+
@param collectionViewLayout The layout object requesting the information.
130+
@param section The index of the section whose header size is being requested.
131+
132+
@return The height of the header. If you return a value of 0, no header is added.
117133
*/
118134
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceHeightForHeaderInSection:(NSInteger)section;
119135

FSQCollectionViewAlignedLayout.m

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)