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
@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
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With