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);

}
有帮助吗?

解决方案

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;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top