Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView estimatedItemSize - last cell is not aligned

I want to make a usual horizontalScrolling flowLayout UICollectionView with estimatedItemSize and preferredLayoutAttributesFittingAttributes in cell. But there is something wrong with last cell. Any idea where is the issue? Project itself

enter image description here

@implementation RowCollectionView  - (instancetype) initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout {     if (self = [super initWithFrame:frame collectionViewLayout:layout])     {         [self configureRowCollectionView];     }      return self; }  - (void) awakeFromNib {     [super awakeFromNib];      [self configureRowCollectionView]; }  - (void) configureRowCollectionView {     self.backgroundColor = [UIColor lightGrayColor];      self.dataSource = self;     self.delegate = self;      // Horizontal Direction     UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *) self.collectionViewLayout;     flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;      // Estimated Item Size     flowLayout.estimatedItemSize = CGSizeMake(self.bounds.size.height, self.bounds.size.height);       [self registerClass:[RowCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([RowCollectionViewCell class])]; }  - (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {     return 10; }  - (UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {     UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([RowCollectionViewCell class]) forIndexPath:indexPath];      cell.contentView.backgroundColor = [UIColor redColor];      return cell; }  @end  @implementation RowCollectionViewCell  - (UICollectionViewLayoutAttributes *) preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {     [super preferredLayoutAttributesFittingAttributes:layoutAttributes];      UICollectionViewLayoutAttributes *attributes = [layoutAttributes copy];      attributes.size = CGSizeMake(80, 80);      return attributes; }  @end     
like image 602
artysx Avatar asked Oct 25 '14 20:10

artysx


2 Answers

I face a similar issue and the problem was solved by giving a proper minimum inter item spacing, using the delegate methods - minimumInteritemSpacingForSectionAt- of UICollectionViewDelegateFlowLayout.

like image 98
Romit Kumar Avatar answered Nov 07 '22 05:11

Romit Kumar


You are setting estimatedItemSize in the init of view itself.

You need to set it in some controller.

Also,

If all of your cells are the same height, use the itemSize property, instead of this property, to specify the cell size instead.

Documentation: estimatedItemSize

like image 31
neelamc23 Avatar answered Nov 07 '22 04:11

neelamc23