gpt4 book ai didi

ios - 带有粘性 header 的 UICollectionView

转载 作者:IT王子 更新时间:2023-10-29 07:50:34 27 4
gpt4 key购买 nike

I found a blog on how to make sticky headers效果很好。唯一的问题是我认为它没有考虑 sectionInserts。

这是它的预期外观:

enter image description here

我有我的插页:

collectionViewFlowLayout.sectionInset = UIEdgeInsetsMake(16, 16, 16, 16);

使用粘性标题,它向下移动 16 个像素:

enter image description here

我尝试修改原始代码,我认为问题出在最后一部分:

layoutAttributes.frame = (CGRect){
.origin = CGPointMake(origin.x, origin.y),
.size = layoutAttributes.frame.size

如果我将它更改为 origin.y - 16,标题将从正确的位置开始,但是当向上推时,头部的 16 个像素会离开屏幕:

enter image description here

我不确定如何让它考虑到 sectionInsects。有人可以帮忙吗?

这是博客中的完整代码:

- (NSArray *) layoutAttributesForElementsInRect:(CGRect)rect {

NSMutableArray *answer = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
UICollectionView * const cv = self.collectionView;
CGPoint const contentOffset = cv.contentOffset;

NSMutableIndexSet *missingSections = [NSMutableIndexSet indexSet];
for (UICollectionViewLayoutAttributes *layoutAttributes in answer) {
if (layoutAttributes.representedElementCategory == UICollectionElementCategoryCell) {
[missingSections addIndex:layoutAttributes.indexPath.section];
}
}
for (UICollectionViewLayoutAttributes *layoutAttributes in answer) {
if ([layoutAttributes.representedElementKind isEqualToString:UICollectionElementKindSectionHeader]) {
[missingSections removeIndex:layoutAttributes.indexPath.section];
}
}

[missingSections enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {

NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:idx];

UICollectionViewLayoutAttributes *layoutAttributes = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:indexPath];

[answer addObject:layoutAttributes];
}];

for (UICollectionViewLayoutAttributes *layoutAttributes in answer) {

if ([layoutAttributes.representedElementKind isEqualToString:UICollectionElementKindSectionHeader]) {

NSInteger section = layoutAttributes.indexPath.section;
NSInteger numberOfItemsInSection = [cv numberOfItemsInSection:section];

NSIndexPath *firstCellIndexPath = [NSIndexPath indexPathForItem:0 inSection:section];
NSIndexPath *lastCellIndexPath = [NSIndexPath indexPathForItem:MAX(0, (numberOfItemsInSection - 1)) inSection:section];

UICollectionViewLayoutAttributes *firstCellAttrs = [self layoutAttributesForItemAtIndexPath:firstCellIndexPath];
UICollectionViewLayoutAttributes *lastCellAttrs = [self layoutAttributesForItemAtIndexPath:lastCellIndexPath];

CGFloat headerHeight = CGRectGetHeight(layoutAttributes.frame);
CGPoint origin = layoutAttributes.frame.origin;
origin.y = MIN(
MAX(
contentOffset.y,
(CGRectGetMinY(firstCellAttrs.frame) - headerHeight)
),
(CGRectGetMaxY(lastCellAttrs.frame) - headerHeight)
);

layoutAttributes.zIndex = 1024;
layoutAttributes.frame = (CGRect){
.origin = origin,
.size = layoutAttributes.frame.size
};
}
}

return answer;
}

最佳答案

适用于 iOS 9 + 的最简单解决方案,因为它不需要编写 UICollectionViewFlowLayout 的子类。

在带有 collectionView 的 viewController 的 viewDidLoad 中使用以下代码:

let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout // casting is required because UICollectionViewLayout doesn't offer header pin. Its feature of UICollectionViewFlowLayout
layout?.sectionHeadersPinToVisibleBounds = true

关于ios - 带有粘性 header 的 UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15233180/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com