UICollectionView layout of last cell
Asked Answered
G

1

10

I have a collectionview which has a UICollectionViewFlowLayout I put 10 items in it. and they have different sizes:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

if (indexPath.row<6) {
    CGSize retval =  CGSizeMake(100, 100);
    return retval;
}

CGSize retval =  CGSizeMake(200, 130);

return retval;
}

it layouts like: first 6 cells look good, and 7-9 are correct, but the last one is not center aligned,

enter image description here enter image description here

any one could explain this to me?

and resent days Im playing with collection layouts, but still feel confused abt the selection of flowlayout and custom layout . any principles on this?

many thx in advance.

Gomel answered 21/7, 2014 at 8:33 Comment(1)
The UICollectionView always tries to fill up as many items as there fit in the row. You can see that in your example: in image 1, there is space for 3 items per row, but in image 2, there is only space for one image. Now the problem in the last row is, that the UICollectionView does not know, whether another item is gonna be there or not, so it aligns to the side and waits for a next element. This is an issue with autoLayout, the views are aligned automatically and not with the layout delegate. (Maybe this helps someone to solve this issue, as it seems to be still open)Morice
D
5

I came across a similar issue with the last few cells in the collection view having an unsightly offset. In my case it was a result of not honoring the section insets with respect to the item sizes.

Try setting the sectionInset of the flowLayout to 0:

flowLayout.sectionInset = UIEdgeInsetsZero;

If this doesn't work you might need to enforce a minimumInterItemSpacing as the flow layout seems to bugger this up on the last few cells:

flowLayout.minimumInteritemSpacing = 10.f;//or any reasonable number
Devoirs answered 17/12, 2014 at 15:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.