diff --git a/UICollectionViewRightAlignedLayout/UICollectionViewRightAlignedLayout.m b/UICollectionViewRightAlignedLayout/UICollectionViewRightAlignedLayout.m index fa3e481..5c872d8 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,12 +59,12 @@ - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSInde UICollectionViewLayoutAttributes* currentItemAttributes = [[super layoutAttributesForItemAtIndexPath:indexPath] copy]; 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; } - + NSIndexPath* previousIndexPath = [NSIndexPath indexPathForItem:indexPath.item-1 inSection:indexPath.section]; CGRect previousFrame = [self layoutAttributesForItemAtIndexPath:previousIndexPath].frame; CGRect currentFrame = currentItemAttributes.frame; @@ -75,12 +75,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]; + [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]; @@ -93,7 +93,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;