From abd25ba3e57a34d734664a4e7cc1eb18043c84a5 Mon Sep 17 00:00:00 2001 From: "Justin.wang" Date: Tue, 21 Jul 2020 10:39:12 +0800 Subject: [PATCH] [iOS]fix: the sticky header in waterfall stays at the bottom when the collectionView scrolls down --- .../Component/Recycler/WXMultiColumnLayout.m | 31 +++++++++++++------ 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/ios/sdk/WeexSDK/Sources/Component/Recycler/WXMultiColumnLayout.m b/ios/sdk/WeexSDK/Sources/Component/Recycler/WXMultiColumnLayout.m index 5cfb14f229..bb2dfd227a 100644 --- a/ios/sdk/WeexSDK/Sources/Component/Recycler/WXMultiColumnLayout.m +++ b/ios/sdk/WeexSDK/Sources/Component/Recycler/WXMultiColumnLayout.m @@ -29,6 +29,7 @@ @interface WXMultiColumnLayoutHeaderAttributes : UICollectionViewLayoutAttributes @property (nonatomic, assign) BOOL isSticky; +@property (nonatomic, assign) CGRect originFrame; @end @@ -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; @@ -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