20
20
21
21
#import " UICollectionViewLeftAlignedLayout.h"
22
22
23
- @interface UICollectionViewLayoutAttributes (LeftAligned)
24
-
25
- - (void )leftAlignFrame ;
26
-
27
- @end
28
-
29
- @implementation UICollectionViewLayoutAttributes (LeftAligned)
23
+ @implementation UICollectionViewLeftAlignedLayout
30
24
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;
36
29
}
37
30
38
- @end
39
-
40
- #pragma mark -
41
-
42
- @implementation UICollectionViewLeftAlignedLayout
43
-
44
31
#pragma mark - UICollectionViewLayout
45
32
46
33
- (NSArray *)layoutAttributesForElementsInRect : (CGRect )rect {
@@ -58,27 +45,28 @@ - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSInde
58
45
UICollectionViewLayoutAttributes* currentItemAttributes = [super layoutAttributesForItemAtIndexPath: indexPath];
59
46
60
47
BOOL isFirstItemInSection = indexPath.item == 0 ;
48
+ CGFloat layoutWidth = CGRectGetWidth (self.collectionView .frame ) - self.sectionInset .left - self.sectionInset .right ;
61
49
62
50
if (isFirstItemInSection) {
63
- [currentItemAttributes leftAlignFrame ];
51
+ [self layoutAttributesAlignLeft: currentItemAttributes ];
64
52
return currentItemAttributes;
65
53
}
66
54
67
55
NSIndexPath * previousIndexPath = [NSIndexPath indexPathForItem: indexPath.item-1 inSection: indexPath.section];
68
56
CGRect previousFrame = [self layoutAttributesForItemAtIndexPath: previousIndexPath].frame ;
69
57
CGFloat previousFrameRightPoint = previousFrame.origin .x + previousFrame.size .width ;
70
58
CGRect currentFrame = currentItemAttributes.frame ;
71
- CGRect strecthedCurrentFrame = CGRectMake (0 ,
59
+ CGRect strecthedCurrentFrame = CGRectMake (self. sectionInset . left ,
72
60
currentFrame.origin .y ,
73
- self. collectionView . frame . size . width ,
61
+ layoutWidth ,
74
62
currentFrame.size .height );
75
63
// if the current frame, once left aligned to the left and stretched to the full collection view
76
64
// widht intersects the previous frame then they are on the same line
77
65
BOOL isFirstItemInRow = !CGRectIntersectsRect (previousFrame, strecthedCurrentFrame);
78
66
79
67
if (isFirstItemInRow) {
80
68
// make sure the first item on a line is left aligned
81
- [currentItemAttributes leftAlignFrame ];
69
+ [self layoutAttributesAlignLeft: currentItemAttributes ];
82
70
return currentItemAttributes;
83
71
}
84
72
0 commit comments