35

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    
Neenu
  • 6,848
  • 2
  • 28
  • 54
artysx
  • 533
  • 5
  • 14

9 Answers9

3

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.

Romit Kumar
  • 2,442
  • 6
  • 21
  • 34
1

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

neelamc23
  • 74
  • 4
1

there is a simple method to resolve this. You can add number of prototype cells to check the cell at required position. Once you find the issue at last cell . Check the cell insets in Inspector window.

Abhi
  • 115
  • 11
1

You call super method but you did not use super returned layoutAttributes.

    [super preferredLayoutAttributesFittingAttributes:layoutAttributes];

You can try to print out original layoutAttributes vs super's layoutAttributes. Sometimes, you don't need to call super function.

Second, You can create custom flowlayout or set inset to let your cell align top. I did this in my project.

Yang Young
  • 602
  • 5
  • 6
0

You can consider it a Suggestion. According to me the height of UICollectionView is more than UICollectionViewCell Height, thats why its happening. please make them equal them

Ajjjjjjjj
  • 669
  • 4
  • 12
0

Custom cell size must be same as that of collection view cell,please check that.It may solve the problem for you.

0

I have done the similar small project (one raw (1*N) horizontal collection view), here is the github. I hope it would be helpful for your requirement.

https://github.com/texas16/HorizontalCollectionView

casillas
  • 16,351
  • 19
  • 115
  • 215
0

I had the same issue and the simplest solution in case you have an horizontal collection having that issue is to make the collection height equal to the items height.

Matan
  • 685
  • 7
  • 18
-1

Had same problem and what fix it in my case was to make sure :

  1. All cells height are equal.

  2. The collectionView height is bigger then cell height + space between cells.

user1105951
  • 2,259
  • 2
  • 34
  • 55