Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

[iOS]fix: the sticky header in waterfall stays at the bottom when scroll down #3244

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
31 changes: 21 additions & 10 deletions ios/sdk/WeexSDK/Sources/Component/Recycler/WXMultiColumnLayout.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
@interface WXMultiColumnLayoutHeaderAttributes : UICollectionViewLayoutAttributes

@property (nonatomic, assign) BOOL isSticky;
@property (nonatomic, assign) CGRect originFrame;

@end

Expand Down Expand Up @@ -162,6 +163,7 @@ - (void)prepareLayout
CGFloat headerHeight = [self.delegate collectionView:[self weakCollectionView] layout:self heightForHeaderInSection:section];
WXMultiColumnLayoutHeaderAttributes *headerAttributes = [WXMultiColumnLayoutHeaderAttributes layoutAttributesForSupplementaryViewOfKind:kCollectionSupplementaryViewKindHeader withIndexPath:[NSIndexPath indexPathForItem:0 inSection:section]];
headerAttributes.frame = CGRectMake(insets.left, currentHeight, self.contentWidth - (insets.left + insets.right), headerHeight);
headerAttributes.originFrame = headerAttributes.frame;
headerAttributes.isSticky = [self.delegate collectionView:[self weakCollectionView] layout:self isNeedStickyForHeaderInSection:section];
headerAttributes.zIndex = headerAttributes.isSticky ? 1 : 0;
headersAttributes[@(section)] = headerAttributes;
Expand Down Expand Up @@ -250,17 +252,26 @@ - (CGSize)collectionViewContentSize
- (void)_adjustStickyForHeaderAttributes:(WXMultiColumnLayoutHeaderAttributes *)header
next:(WXMultiColumnLayoutHeaderAttributes *)nextHeader
{
CGRect bounds = [self weakCollectionView].bounds;
CGFloat originY = header.frame.origin.y;
CGFloat maxY = nextHeader ? (nextHeader.frame.origin.y - header.frame.size.height) : (CGRectGetMaxY(bounds) - header.frame.size.height);
CGFloat currentY = CGRectGetMaxY(bounds) - bounds.size.height + [self weakCollectionView].contentInset.top;

CGFloat resultY = MIN(MAX(currentY, originY), maxY);
CGPoint origin = header.frame.origin;
origin.y = resultY;
NSInteger section = header.indexPath.section;
CGRect frame = header.frame;
BOOL isNeedChangeFrame = NO;
CGFloat offsetY = self.collectionView.contentOffset.y ;
if (offsetY > header.originFrame.origin.y &&
offsetY < (nextHeader ? nextHeader.frame.origin.y : self.collectionView.contentSize.height)) {
frame.origin.y = offsetY;
header.zIndex = 1000+section;
header.frame = frame;
isNeedChangeFrame = YES;
}

header.frame = (CGRect){origin, header.frame.size};
header.hidden = NO;
if (!isNeedChangeFrame) {
/*
这里需要注意,在悬浮的情况下改变了headerAtt的frame
在滑出header又滑回来时,headerAtt已经被修改过,需要改回原始值
否则header无法正确归位
*/
header.frame = header.originFrame;
}
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath
Expand Down