Skip to content

Commit 25b1093

Browse files
committed
Add sectionInset support.
1 parent dd7e84b commit 25b1093

File tree

1 file changed

+10
-22
lines changed

1 file changed

+10
-22
lines changed

UICollectionViewLeftAlignedLayout/UICollectionViewLeftAlignedLayout.m

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,14 @@
2020

2121
#import "UICollectionViewLeftAlignedLayout.h"
2222

23-
@interface UICollectionViewLayoutAttributes (LeftAligned)
24-
25-
- (void)leftAlignFrame;
26-
27-
@end
28-
29-
@implementation UICollectionViewLayoutAttributes (LeftAligned)
23+
@implementation UICollectionViewLeftAlignedLayout
3024

31-
- (void)leftAlignFrame
32-
{
33-
CGRect frame = self.frame;
34-
frame.origin.x = 0;
35-
self.frame = frame;
25+
- (void)layoutAttributesAlignLeft:(UICollectionViewLayoutAttributes *)layoutAttributes {
26+
CGRect frame = layoutAttributes.frame;
27+
frame.origin.x = self.sectionInset.left;
28+
layoutAttributes.frame = frame;
3629
}
3730

38-
@end
39-
40-
#pragma mark -
41-
42-
@implementation UICollectionViewLeftAlignedLayout
43-
4431
#pragma mark - UICollectionViewLayout
4532

4633
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
@@ -58,27 +45,28 @@ - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSInde
5845
UICollectionViewLayoutAttributes* currentItemAttributes = [super layoutAttributesForItemAtIndexPath:indexPath];
5946

6047
BOOL isFirstItemInSection = indexPath.item == 0;
48+
CGFloat layoutWidth = CGRectGetWidth(self.collectionView.frame) - self.sectionInset.left - self.sectionInset.right;
6149

6250
if (isFirstItemInSection) {
63-
[currentItemAttributes leftAlignFrame];
51+
[self layoutAttributesAlignLeft:currentItemAttributes];
6452
return currentItemAttributes;
6553
}
6654

6755
NSIndexPath* previousIndexPath = [NSIndexPath indexPathForItem:indexPath.item-1 inSection:indexPath.section];
6856
CGRect previousFrame = [self layoutAttributesForItemAtIndexPath:previousIndexPath].frame;
6957
CGFloat previousFrameRightPoint = previousFrame.origin.x + previousFrame.size.width;
7058
CGRect currentFrame = currentItemAttributes.frame;
71-
CGRect strecthedCurrentFrame = CGRectMake(0,
59+
CGRect strecthedCurrentFrame = CGRectMake(self.sectionInset.left,
7260
currentFrame.origin.y,
73-
self.collectionView.frame.size.width,
61+
layoutWidth,
7462
currentFrame.size.height);
7563
// if the current frame, once left aligned to the left and stretched to the full collection view
7664
// widht intersects the previous frame then they are on the same line
7765
BOOL isFirstItemInRow = !CGRectIntersectsRect(previousFrame, strecthedCurrentFrame);
7866

7967
if (isFirstItemInRow) {
8068
// make sure the first item on a line is left aligned
81-
[currentItemAttributes leftAlignFrame];
69+
[self layoutAttributesAlignLeft:currentItemAttributes];
8270
return currentItemAttributes;
8371
}
8472

0 commit comments

Comments
 (0)