Last active
January 25, 2019 11:39
-
-
Save nicksnyder/4075682 to your computer and use it in GitHub Desktop.
Fix rendering bug in UICollectionViewFlowLayout as described here:
http://stackoverflow.com/questions/12927027/uicollectionview-flowlayout-not-wrapping-cells-correctly-ios
This file contains 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
// Created by Nick Snyder on 11/13/12. | |
// https://gist.github.com/nicksnyder/4075682 | |
// http://stackoverflow.com/questions/12927027/uicollectionview-flowlayout-not-wrapping-cells-correctly-ios | |
// NDCollectionViewFlowLayout.h | |
@interface NDCollectionViewFlowLayout : UICollectionViewFlowLayout | |
@end |
This file contains 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
// Created by Nick Snyder on 11/13/12. | |
// https://gist.github.com/nicksnyder/4075682 | |
// http://stackoverflow.com/questions/12927027/uicollectionview-flowlayout-not-wrapping-cells-correctly-ios | |
// NDCollectionViewFlowLayout.m | |
#import "NDCollectionViewFlowLayout.h" | |
@implementation NDCollectionViewFlowLayout | |
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect { | |
NSArray *attributes = [super layoutAttributesForElementsInRect:rect]; | |
NSMutableArray *newAttributes = [NSMutableArray arrayWithCapacity:attributes.count]; | |
for (UICollectionViewLayoutAttributes *attribute in attributes) { | |
if ((attribute.frame.origin.x + attribute.frame.size.width <= self.collectionViewContentSize.width) && | |
(attribute.frame.origin.y + attribute.frame.size.height <= self.collectionViewContentSize.height)) { | |
[newAttributes addObject:attribute]; | |
} | |
} | |
return newAttributes; | |
} | |
@end |
@steipete Is this fix in PSTCollectionView ?
Thanks very much!
I had to take into account the section inset in order for it to work for me:
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
NSArray *attributes = [super layoutAttributesForElementsInRect:rect];
NSMutableArray *newAttributes = [NSMutableArray arrayWithCapacity:attributes.count];
for (UICollectionViewLayoutAttributes *attribute in attributes) {
if ((attribute.representedElementCategory != UICollectionElementCategoryCell) || // always include everything that's not a cell
((attribute.frame.origin.x + attribute.frame.size.width <= (self.collectionViewContentSize.width - self.sectionInset.right)) &&
(attribute.frame.origin.y + attribute.frame.size.height <= (self.collectionViewContentSize.height - self.sectionInset.bottom))))
{
[newAttributes addObject:attribute];
}
}
return newAttributes;
}
Wonderful~
How can I implement this? I already made this and it is not called. Where should I call this? Thanks in advance
How can I implement this? I already made this and it is not called. Where should I call this? Thanks in advance
Click here for set collectionview flow layout
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Relevant SO article: http://stackoverflow.com/questions/12927027/uicollectionview-flowlayout-not-wrapping-cells-correctly-ios