Bir koleksiyon görünümünün en üstte olduğu bir koleksiyon çoğaltmaya çalışıyorum, koleksiyon görünümünün ilk satırının üst kısmındaki bir başlık görünümü, ardından bir başlık bölümü görünümü (indexPath.section == 1 için), örtüşen veya altında gitmeden başlık görünümüne yapışır.UICollectionView Bölüm Üstbilgisi Çubuğa belirli bir Y
Başlığı yapışkan hale getirmek için özel bir düzen uygulamıyorum. Ancak, başlık görünümüne sadık kalamayım ('origin.y + height'dan sonra), koleksiyon görünümünün en üstüne yapışır. kaydırma önce
:
özel düzeni ile bağlantılı: Burada
İdeal senaryo
İşte benim IB yapısı var
kaydırma sonra:
@implementation LLCollectionCustomFlowLayout
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
return YES;
}
- (NSArray *) layoutAttributesForElementsInRect:(CGRect)rect {
NSMutableArray *answer = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
NSMutableIndexSet *missingSections = [NSMutableIndexSet indexSet];
for (NSUInteger idx=0; idx<[answer count]; idx++) {
UICollectionViewLayoutAttributes *layoutAttributes = answer[idx];
if (layoutAttributes.representedElementCategory == UICollectionElementCategoryCell) {
[missingSections addIndex:layoutAttributes.indexPath.section]; // remember that we need to layout header for this section
}
if ([layoutAttributes.representedElementKind isEqualToString:UICollectionElementKindSectionHeader]) {
[answer removeObjectAtIndex:idx]; // remove layout of header done by our super, we will do it right later
idx--;
}
}
// layout all headers needed for the rect using self code
[missingSections enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:idx];
UICollectionViewLayoutAttributes *layoutAttributes = [self layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:indexPath];
if (layoutAttributes != nil) {
[answer addObject:layoutAttributes];
}
}];
return answer;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
UICollectionViewLayoutAttributes *attributes = [super layoutAttributesForSupplementaryViewOfKind:kind atIndexPath:indexPath];
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
UICollectionView * const cv = self.collectionView;
CGPoint const contentOffset = cv.contentOffset;
CGPoint nextHeaderOrigin = CGPointMake(INFINITY, INFINITY);
if (indexPath.section+1 < [cv numberOfSections]) {
UICollectionViewLayoutAttributes *nextHeaderAttributes = [super layoutAttributesForSupplementaryViewOfKind:kind atIndexPath:[NSIndexPath indexPathForItem:0 inSection:indexPath.section+1]];
nextHeaderOrigin = nextHeaderAttributes.frame.origin;
}
CGRect frame = attributes.frame;
if (self.scrollDirection == UICollectionViewScrollDirectionVertical) {
frame.origin.y = MIN(MAX(contentOffset.y, frame.origin.y), nextHeaderOrigin.y - CGRectGetHeight(frame));
}
else { // UICollectionViewScrollDirectionHorizontal
frame.origin.x = MIN(MAX(contentOffset.x, frame.origin.x), nextHeaderOrigin.x - CGRectGetWidth(frame));
}
attributes.zIndex = 1024;
attributes.frame = frame;
}
return attributes;
}
- (UICollectionViewLayoutAttributes *)initialLayoutAttributesForAppearingSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForSupplementaryViewOfKind:kind atIndexPath:indexPath];
return attributes;
}
- (UICollectionViewLayoutAttributes *)finalLayoutAttributesForDisappearingSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForSupplementaryViewOfKind:kind atIndexPath:indexPath];
return attributes;
}
@end
Cevabınız için teşekkür ederiz. Yine de çalışmıyor. İki derleyici uyarısını düzelttikten sonra (prevHeaderMaxY ve prevHeaderMaxX, CGFloats olmalıdır), başlık artık üstte kalmaz. Bunun bir bölüm [0] üstbilgisinin yokluğundan kaynaklandığına inanıyorum. Nil olduğu gibi, yapıştırma noktasını bulamıyor. – jonypz
İlk üstbilgiyi geri koydum ve hala çalışmıyor. Şimdi başlıklar artık yapışmıyor. – jonypz
Kullanmaya çalışın UICollectionViewLayoutAttributes * prevHeaderAttributes = [self layoutAttributesFor ... 'yerine UICollectionViewLayoutAttributes * prevHeaderAttributes = [super ...'. Süper arama, önceki sürümün doğru özelliklerini döndürmez. başlık – sgl0v