How can I get two columns in a UICollectionView that take up the whole space without any margins or gaps much like the picture I drew below:
I tried using the following code:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(160, 160);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 1.0;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 1.0;
}
but got this result:
By Implementing UICollectionViewDelegateFlowLayout protocol, you can find many methods for implementing layout of UICollectionView.
collectionView:layout:minimumInteritemSpacingForSectionAtIndex:
collectionView:layout:minimumLineSpacingForSectionAtIndex:
should be enough to solve this.
Also you can implement, numberOfItems and numberOfSections to specify number of row, column.
Alternatively, you can specify the size of your cells as (width of UICollectionView) / (number of columns you want) - some offset between cells
in cellForItemAtIndexPath.
By default the cells are calculated by this and arranged accordingly. Just by decreasing the size you will see multi columns. Ref1 Ref2
The best way to do this I found is either in Storyboard or in the delegate methods, have an item size of 159 and a gap inbetween gap of 0.5 or even smaller if you want. Just play around with it
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