Skip to content

Commit a722be1

Browse files
Merge pull request #102 from kishikawakatsumi/estimated-supplementary
Fix supplementary view does not anchor with estimated sized cells
2 parents a123fd6 + 572f27b commit a722be1

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

Sources/IBPCollectionViewCompositionalLayout/IBPUICollectionViewCompositionalLayout.m

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -239,16 +239,16 @@ - (void)prepareLayout {
239239

240240
NSMutableDictionary<NSString *, UICollectionViewLayoutAttributes *> *supplementaryAttributes = cachedSupplementaryAttributes;
241241
[layoutItem enumerateSupplementaryItemsWithHandler:^(IBPNSCollectionLayoutSupplementaryItem * _Nonnull supplementaryItem, BOOL * _Nonnull stop) {
242-
UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:supplementaryItem.elementKind withIndexPath:indexPath];
242+
UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:supplementaryItem.elementKind withIndexPath:indexPath];
243243

244-
IBPNSCollectionLayoutContainer *itemContainer = [[IBPNSCollectionLayoutContainer alloc] initWithContentSize:cellFrame.size
245-
contentInsets:IBPNSDirectionalEdgeInsetsZero];
246-
CGSize itemSize = [supplementaryItem.layoutSize effectiveSizeForContainer:itemContainer];
247-
CGRect itemFrame = [supplementaryItem.containerAnchor itemFrameForContainerRect:cellFrame itemSize:itemSize itemLayoutAnchor:supplementaryItem.itemAnchor];
248-
layoutAttributes.frame = itemFrame;
249-
layoutAttributes.zIndex = supplementaryItem.zIndex;
244+
IBPNSCollectionLayoutContainer *container = [[IBPNSCollectionLayoutContainer alloc] initWithContentSize:cellFrame.size
245+
contentInsets:IBPNSDirectionalEdgeInsetsZero];
246+
CGSize size = [supplementaryItem.layoutSize effectiveSizeForContainer:container];
247+
CGRect frame = [supplementaryItem.containerAnchor itemFrameForContainerRect:cellFrame itemSize:size itemLayoutAnchor:supplementaryItem.itemAnchor];
248+
attributes.frame = frame;
249+
attributes.zIndex = supplementaryItem.zIndex;
250250

251-
supplementaryAttributes[[NSString stringWithFormat:@"%@-%zd-%zd", supplementaryItem.elementKind, indexPath.section, indexPath.item]] = layoutAttributes;
251+
supplementaryAttributes[[NSString stringWithFormat:@"%@-%zd-%zd", supplementaryItem.elementKind, indexPath.section, indexPath.item]] = attributes;
252252
}];
253253
}
254254

@@ -575,7 +575,7 @@ - (UICollectionViewLayoutAttributes *)prepareLayoutForBoundaryItem:(IBPNSCollect
575575

576576
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
577577
NSMutableArray<UICollectionViewLayoutAttributes *> *layoutAttributes = [[NSMutableArray alloc] init];
578-
NSArray *sortedAttributes = [cachedItemAttributes.allValues sortedArrayUsingComparator:^NSComparisonResult(UICollectionViewLayoutAttributes * _Nonnull attrs1, UICollectionViewLayoutAttributes * _Nonnull attrs2) {
578+
NSArray *itemAttributes = [cachedItemAttributes.allValues sortedArrayUsingComparator:^NSComparisonResult(UICollectionViewLayoutAttributes * _Nonnull attrs1, UICollectionViewLayoutAttributes * _Nonnull attrs2) {
579579
switch (self.scrollDirection) {
580580
case UICollectionViewScrollDirectionVertical:
581581
return CGRectGetMinY(attrs1.frame) > CGRectGetMinY(attrs2.frame);
@@ -584,8 +584,8 @@ - (UICollectionViewLayoutAttributes *)prepareLayoutForBoundaryItem:(IBPNSCollect
584584
}
585585
}];
586586

587-
for (NSInteger i = 0; i < sortedAttributes.count; i++) {
588-
UICollectionViewLayoutAttributes *attributes = sortedAttributes[i];
587+
for (NSInteger i = 0; i < itemAttributes.count; i++) {
588+
UICollectionViewLayoutAttributes *attributes = itemAttributes[i];
589589
if (!CGRectIntersectsRect(attributes.frame, rect)) {
590590
continue;
591591
}
@@ -623,8 +623,8 @@ - (UICollectionViewLayoutAttributes *)prepareLayoutForBoundaryItem:(IBPNSCollect
623623

624624
CGRect frame = attributes.frame;
625625
if (CGRectGetWidth(attributes.frame) != fitSize.width || CGRectGetHeight(attributes.frame) != fitSize.height) {
626-
for (NSInteger j = i + 1; j < sortedAttributes.count; j++) {
627-
UICollectionViewLayoutAttributes *nextAttributes = sortedAttributes[j];
626+
for (NSInteger j = i + 1; j < itemAttributes.count; j++) {
627+
UICollectionViewLayoutAttributes *nextAttributes = itemAttributes[j];
628628
CGRect nextFrame = nextAttributes.frame;
629629

630630
switch (self.scrollDirection) {
@@ -645,10 +645,26 @@ - (UICollectionViewLayoutAttributes *)prepareLayoutForBoundaryItem:(IBPNSCollect
645645

646646
frame.size = fitSize;
647647
attributes.frame = frame;
648+
[layoutAttributes addObject:attributes];
648649

649650
contentFrame = CGRectUnion(contentFrame, frame);
650651

651-
[layoutAttributes addObject:attributes];
652+
NSMutableDictionary<NSString *, UICollectionViewLayoutAttributes *> *supplementaryAttributes = cachedSupplementaryAttributes;
653+
[layoutItem enumerateSupplementaryItemsWithHandler:^(IBPNSCollectionLayoutSupplementaryItem * _Nonnull supplementaryItem, BOOL * _Nonnull stop) {
654+
UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind:supplementaryItem.elementKind withIndexPath:indexPath];
655+
656+
IBPNSCollectionLayoutContainer *container = [[IBPNSCollectionLayoutContainer alloc] initWithContentSize:frame.size
657+
contentInsets:IBPNSDirectionalEdgeInsetsZero];
658+
CGSize size = [supplementaryItem.layoutSize effectiveSizeForContainer:container];
659+
CGRect supplementaryFrame = [supplementaryItem.containerAnchor itemFrameForContainerRect:frame itemSize:size itemLayoutAnchor:supplementaryItem.itemAnchor];
660+
attributes.frame = supplementaryFrame;
661+
attributes.zIndex = supplementaryItem.zIndex;
662+
663+
supplementaryAttributes[[NSString stringWithFormat:@"%@-%zd-%zd", supplementaryItem.elementKind, indexPath.section, indexPath.item]] = attributes;
664+
665+
[layoutAttributes addObject:attributes];
666+
}];
667+
652668
continue;
653669
}
654670
}

0 commit comments

Comments
 (0)