Skip to content

Instantly share code, notes, and snippets.

@onmyway133
Created June 15, 2015 08:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onmyway133/0efba5e49402040c1a5c to your computer and use it in GitHub Desktop.
Save onmyway133/0efba5e49402040c1a5c to your computer and use it in GitHub Desktop.
DNLastItemCenteredLayout.m
#import "DNLastItemCenteredLayout.h"
@implementation DNLastItemCenteredLayout
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
NSArray *attributes = [super layoutAttributesForElementsInRect:rect];
for (UICollectionViewLayoutAttributes *attribute in attributes) {
NSInteger itemCount = [self.collectionView.dataSource collectionView:self.collectionView
numberOfItemsInSection:attribute.indexPath.section];
if (itemCount % 2 == 1 && attribute.indexPath.item == itemCount - 1) {
CGRect originalFrame = attribute.frame;
attribute.frame = CGRectMake(self.collectionView.bounds.size.width/2-originalFrame.size.width/2,
originalFrame.origin.y,
originalFrame.size.width,
originalFrame.size.height);
}
}
return attributes;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment