Skip to content

Instantly share code, notes, and snippets.

@alloy
Last active July 18, 2020 22:02
Show Gist options
  • Save alloy/5460654 to your computer and use it in GitHub Desktop.
Save alloy/5460654 to your computer and use it in GitHub Desktop.
UICollectionViewFlowLayout subclass to vertically align cells at the bottom edge.
@interface MTLibraryCollectionViewFlowLayout : UICollectionViewFlowLayout
@end
@implementation MTLibraryCollectionViewFlowLayout
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect;
{
NSArray *attrs = [super layoutAttributesForElementsInRect:rect];
for (UICollectionViewLayoutAttributes *element in attrs) {
if (element.representedElementCategory == UICollectionElementCategoryCell) {
CGRect frame = element.frame;
// Get the diff between the ideal height and the actual thumbnail height.
CGFloat deltaY = self.itemSize.height - CGRectGetHeight(frame);
// As the element is already centered, the delta should take that into account.
deltaY = ceilf(deltaY / 2.0);
// Offset and be done with it.
element.frame = CGRectOffset(frame, 0, deltaY);
}
}
return attrs;
}
@end
@OskarGroth
Copy link

Works great in theory but is still a hack and can't be animated between layouts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment