Last active
July 18, 2020 22:02
-
-
Save alloy/5460654 to your computer and use it in GitHub Desktop.
UICollectionViewFlowLayout subclass to vertically align cells at the bottom edge.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface MTLibraryCollectionViewFlowLayout : UICollectionViewFlowLayout | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 |
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
Great idea!