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:
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;
}
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