Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two columns on an uicollectionview

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:enter image description here

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:

enter image description here

like image 547
jacobsieradzki Avatar asked Apr 26 '14 22:04

jacobsieradzki


2 Answers

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

like image 156
Vacca Avatar answered Nov 19 '22 12:11

Vacca


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

like image 4
jacobsieradzki Avatar answered Nov 19 '22 11:11

jacobsieradzki