From 8fca27285e7c79d4dbcc3d2b2867d70247c2f69a Mon Sep 17 00:00:00 2001 From: Hesham Abd-Elmegid Date: Wed, 24 Dec 2014 20:32:32 +0200 Subject: [PATCH 1/2] Takes collection view right content inset into account when calculating cell frames. --- .../UICollectionViewRightAlignedLayout.m | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/UICollectionViewRightAlignedLayout/UICollectionViewRightAlignedLayout.m b/UICollectionViewRightAlignedLayout/UICollectionViewRightAlignedLayout.m index 644fdfc..b2dd873 100644 --- a/UICollectionViewRightAlignedLayout/UICollectionViewRightAlignedLayout.m +++ b/UICollectionViewRightAlignedLayout/UICollectionViewRightAlignedLayout.m @@ -21,16 +21,16 @@ @interface UICollectionViewLayoutAttributes (RightAligned) -- (void)rightAlignFrameOnWidth:(CGFloat)width; +- (void)rightAlignFrameOnWidth:(CGFloat)width withSectionInset:(UIEdgeInsets)sectionInset; @end @implementation UICollectionViewLayoutAttributes (RightAligned) -- (void)rightAlignFrameOnWidth:(CGFloat)width +- (void)rightAlignFrameOnWidth:(CGFloat)width withSectionInset:(UIEdgeInsets)sectionInset { CGRect frame = self.frame; - frame.origin.x = width - frame.size.width; + frame.origin.x = width - frame.size.width - sectionInset.right; self.frame = frame; } @@ -59,7 +59,7 @@ - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSInde BOOL isFirstItemInSection = indexPath.item == 0; if (isFirstItemInSection) { - [currentItemAttributes rightAlignFrameOnWidth:self.collectionView.frame.size.width]; + [currentItemAttributes rightAlignFrameOnWidth:self.collectionView.frame.size.width withSectionInset:self.sectionInset]; return currentItemAttributes; } @@ -75,7 +75,7 @@ - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSInde BOOL isFirstItemInRow = !CGRectIntersectsRect(previousFrame, strecthedCurrentFrame); if (isFirstItemInRow) { - [currentItemAttributes rightAlignFrameOnWidth:self.collectionView.frame.size.width]; + [currentItemAttributes rightAlignFrameOnWidth:self.collectionView.frame.size.width withSectionInset:self.sectionInset]; return currentItemAttributes; } From 5f7731908379dda2fe9e17dbc387a03b14358b0c Mon Sep 17 00:00:00 2001 From: Hesham Abd-Elmegid Date: Tue, 1 Sep 2015 17:50:09 +0200 Subject: [PATCH 2/2] Fixes warning about modifying layout attributes without copying them. --- .../UICollectionViewRightAlignedLayout.m | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/UICollectionViewRightAlignedLayout/UICollectionViewRightAlignedLayout.m b/UICollectionViewRightAlignedLayout/UICollectionViewRightAlignedLayout.m index b2dd873..17a41d1 100644 --- a/UICollectionViewRightAlignedLayout/UICollectionViewRightAlignedLayout.m +++ b/UICollectionViewRightAlignedLayout/UICollectionViewRightAlignedLayout.m @@ -43,26 +43,32 @@ @implementation UICollectionViewRightAlignedLayout #pragma mark - UICollectionViewLayout - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect { - NSArray* attributesToReturn = [super layoutAttributesForElementsInRect:rect]; - for (UICollectionViewLayoutAttributes* attributes in attributesToReturn) { - if (nil == attributes.representedElementKind) { - NSIndexPath* indexPath = attributes.indexPath; - attributes.frame = [self layoutAttributesForItemAtIndexPath:indexPath].frame; + NSMutableArray *returnAttributesArray = [NSMutableArray array]; + NSArray *originalAttributesArray = [[super layoutAttributesForElementsInRect:rect] copy]; + + for (UICollectionViewLayoutAttributes *attributes in originalAttributesArray) { + if (!attributes.representedElementKind) { + NSIndexPath *indexPath = attributes.indexPath; + UICollectionViewLayoutAttributes *returnAttributes = [attributes copy]; + returnAttributes.frame = [self layoutAttributesForItemAtIndexPath:indexPath].frame; + + [returnAttributesArray addObject:returnAttributes]; } } - return attributesToReturn; + + return returnAttributesArray; } - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath { - UICollectionViewLayoutAttributes* currentItemAttributes = [super layoutAttributesForItemAtIndexPath:indexPath]; - + UICollectionViewLayoutAttributes* currentItemAttributes = [[super layoutAttributesForItemAtIndexPath:indexPath] copy]; + BOOL isFirstItemInSection = indexPath.item == 0; - + if (isFirstItemInSection) { [currentItemAttributes rightAlignFrameOnWidth:self.collectionView.frame.size.width withSectionInset:self.sectionInset]; return currentItemAttributes; } - + NSIndexPath* previousIndexPath = [NSIndexPath indexPathForItem:indexPath.item-1 inSection:indexPath.section]; CGRect previousFrame = [self layoutAttributesForItemAtIndexPath:previousIndexPath].frame; CGRect currentFrame = currentItemAttributes.frame; @@ -73,12 +79,12 @@ - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSInde // if the current frame, once left aligned to the left and stretched to the full collection view // widht intersects the previous frame then they are on the same line BOOL isFirstItemInRow = !CGRectIntersectsRect(previousFrame, strecthedCurrentFrame); - + if (isFirstItemInRow) { [currentItemAttributes rightAlignFrameOnWidth:self.collectionView.frame.size.width withSectionInset:self.sectionInset]; return currentItemAttributes; } - + CGFloat previousFrameLeftPoint = previousFrame.origin.x; CGRect frame = currentItemAttributes.frame; CGFloat minimumInteritemSpacing = [self evaluatedMinimumInteritemSpacingForItemAtIndex:indexPath.row]; @@ -91,7 +97,7 @@ - (CGFloat)evaluatedMinimumInteritemSpacingForItemAtIndex:(NSInteger)index { if ([self.collectionView.delegate respondsToSelector:@selector(collectionView:layout:minimumInteritemSpacingForSectionAtIndex:)]) { id delegate = (id)self.collectionView.delegate; - + return [delegate collectionView:self.collectionView layout:self minimumInteritemSpacingForSectionAtIndex:index]; } else { return self.minimumInteritemSpacing;