Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] enable sticky header #188

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHTCollectionViewWaterfallLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,16 @@ extern NSString *const CHTCollectionElementKindSectionFooter;
*/
@property (nonatomic, assign) UIEdgeInsets sectionInset;


/**
* @brief control when user scroll the collecitonView , sectionHeader stay at top the screen
*
*
*/
@property (nonatomic, assign) BOOL enableStickyHeader;



/**
* @brief The direction in which items will be rendered in subsequent rows.
* @discussion
Expand Down
34 changes: 29 additions & 5 deletions CHTCollectionViewWaterfallLayout.m
Original file line number Diff line number Diff line change
Expand Up @@ -465,18 +465,42 @@ - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
}
}

if (self.enableStickyHeader) {
for (int i = 0; i < self.allItemAttributes.count; i++) {
UICollectionViewLayoutAttributes *attr = self.allItemAttributes[i];
if ([attr.representedElementKind isEqualToString:CHTCollectionElementKindSectionHeader]) {
NSInteger section = attr.indexPath.section;
NSInteger numberOfItemsInSection = [self.collectionView numberOfItemsInSection:section];
NSIndexPath *firstCellIndexPath = [NSIndexPath indexPathForItem:0 inSection:section];
NSIndexPath *lastCellIndexPath = [NSIndexPath indexPathForItem:MAX(0, numberOfItemsInSection - 1) inSection:section];
UICollectionViewLayoutAttributes *firstCellAttributes = [self layoutAttributesForItemAtIndexPath:firstCellIndexPath];
UICollectionViewLayoutAttributes *lastCellAttributes = [self layoutAttributesForItemAtIndexPath:lastCellIndexPath];
CGFloat headerHeight = CGRectGetHeight(attr.frame);
CGPoint origin = attr.frame.origin;
CGFloat y1 = MAX(self.collectionView.contentOffset.y, (CGRectGetMinY(firstCellAttributes.frame) - headerHeight));
CGFloat y2 = CGRectGetMaxY(lastCellAttributes.frame) - headerHeight;
origin.y = MIN(y1, y2);
attr.zIndex = 1024;
attr.frame = CGRectMake(origin.x, origin.y, CGRectGetWidth(attr.frame), CGRectGetHeight(attr.frame));
supplHeaderAttrDict[attr.indexPath] = attr;
}
}
}
NSArray *result = [cellAttrDict.allValues arrayByAddingObjectsFromArray:supplHeaderAttrDict.allValues];
result = [result arrayByAddingObjectsFromArray:supplFooterAttrDict.allValues];
result = [result arrayByAddingObjectsFromArray:decorAttrDict.allValues];
return result;
}

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
CGRect oldBounds = self.collectionView.bounds;
if (CGRectGetWidth(newBounds) != CGRectGetWidth(oldBounds)) {
return YES;
}
return NO;
if (self.enableStickyHeader) {
return YES;
}
CGRect oldBounds = self.collectionView.bounds;
if (CGRectGetWidth(newBounds) != CGRectGetWidth(oldBounds)) {
return YES;
}
return NO;
}

#pragma mark - Private Methods
Expand Down
2 changes: 1 addition & 1 deletion Demo/Objective-C/Demo/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ @implementation ViewController
- (UICollectionView *)collectionView {
if (!_collectionView) {
CHTCollectionViewWaterfallLayout *layout = [[CHTCollectionViewWaterfallLayout alloc] init];

layout.enableStickyHeader = YES;
layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
layout.headerHeight = 15;
layout.footerHeight = 10;
Expand Down
23 changes: 11 additions & 12 deletions Demo/Objective-C/Demo/en.lproj/MainStoryboard_iPhone.storyboard
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="4514" systemVersion="13A603" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" initialViewController="2">
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" colorMatched="YES" initialViewController="2">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment defaultVersion="1536" identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3746"/>
<deployment version="1792" identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14460.20"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="5">
<objects>
<viewController id="2" customClass="ViewController" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="3">
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</view>
<extendedEdge key="edgesForExtendedLayout" bottom="YES"/>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="4" sceneMemberID="firstResponder"/>
</objects>
</scene>
</scenes>
<simulatedMetricsContainer key="defaultSimulatedMetrics">
<simulatedStatusBarMetrics key="statusBar"/>
<simulatedOrientationMetrics key="orientation"/>
<simulatedScreenMetrics key="destination" type="retina4"/>
</simulatedMetricsContainer>
</document>
</document>