2

I created a collection view with custom collection cell.set with flowlayout as

UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
[flowLayout setItemSize:CGSizeMake(250, 480)];
[flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[self.templateCollectionView setCollectionViewLayout:flowLayout];

I set it horizontal but still a cell is misplaced in figure see att mmp misplaced & it will back in its correct position if we scroll and load it back. Can anyone suggest whats wrong in this ?

collection

Mayur
  • 5,004
  • 7
  • 34
  • 62
Lithu T.V
  • 19,491
  • 11
  • 53
  • 98

2 Answers2

2

There is a work around on this answer that works for me. Below is the fix I used edited for horizontal scrolling. Basically just filtering out the attributes item that is providing the bad frame down below.

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
     NSArray *attributes = [super layoutAttributesForElementsInRect:rect];
     NSMutableArray *newAttributes = [NSMutableArray arrayWithCapacity:attributes.count];
     for (UICollectionViewLayoutAttributes *attribute in attributes) {
          if (attribute.frame.origin.y + attribute.frame.size.height <=    self.collectionViewContentSize.height) {
               [newAttributes addObject:attribute];
          }
     }
     return newAttributes;
}
Community
  • 1
  • 1
Samuel
  • 391
  • 1
  • 10
0

It generally happens when we haven't given proper cell spacing. Make sure, while populating cells of uicollectionview, cell spacing should be given. Also go through UICollectionView flowLayout not wrapping cells correctly (iOS) may it help you out.

Community
  • 1
  • 1
Arun
  • 475
  • 6
  • 20