Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Last UICollectionViewCell alignment

I'm having problem aligning my UICollectionViewCells. I have 6 cells which are align perfectly. However when I change the size of any one, the last cell loses its horizontal alignment and I have no idea why. This is the result when I change any cell height:

enter image description here

As you can see, in this example I'm changing the height of the 3rd cell and the last cell is no longer aligned horizontally. Here is my code:

- (void)viewDidLoad
{
...
FlowLayout *flowLayout = [[FlowLayout alloc] init];
flowLayout.itemSize = CGSizeMake(152, 260);
flowLayout.sectionInset = UIEdgeInsetsMake( 5, 5, 5, 5);
flowLayout.minimumInteritemSpacing = 2.0f;
flowLayout.minimumLineSpacing = 8.0f;
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
self.collectionView.collectionViewLayout = flowLayout;
...
}

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

if (indexPath.row == 2){
    return CGSizeMake(152, 200);
}
return CGSizeMake(152, 270);

}
like image 945
pochimen Avatar asked May 01 '14 03:05

pochimen


1 Answers

Try using minimumInteritemSpacingForSectionAtIndex method for setting the item space instead of doing it in viewDidLoad.

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout    minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {

return 5.0;
}
like image 140
rshankar Avatar answered Oct 16 '22 20:10

rshankar