Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
Expand All @@ -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];
Expand All @@ -93,7 +93,7 @@ - (CGFloat)evaluatedMinimumInteritemSpacingForItemAtIndex:(NSInteger)index
{
if ([self.collectionView.delegate respondsToSelector:@selector(collectionView:layout:minimumInteritemSpacingForSectionAtIndex:)]) {
id<UICollectionViewDelegateRightAlignedLayout> delegate = (id<UICollectionViewDelegateRightAlignedLayout>)self.collectionView.delegate;

return [delegate collectionView:self.collectionView layout:self minimumInteritemSpacingForSectionAtIndex:index];
} else {
return self.minimumInteritemSpacing;
Expand Down