Вопрос

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

Это было полезно?

Решение 2

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

Другие советы

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

to first have set your desired indentation in viewDidLoad, for example, put the padding on 10

let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
clnView.collectionViewLayout = layout

and then use this

func collectionView(collectionView: UICollectionView,
        layout collectionViewLayout: UICollectionViewLayout,
        sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
            //device screen size
            let width = UIScreen.mainScreen().bounds.size.width
            //calculation of cell size
            return CGSize(width: ((width / 2) - 15)   , height: 155)
    }

@jacobsieradzki I think you need to do some changes in the size of your cells, try first to make them smaller, I'm thinking that probably 2 cells of 160.0 width + 1.0 of interline spacing is what is causing your problem.
I've followed a Ray Wenderlich tutorial some days ago that was really easy to follow and explains very clear the concepts behind UICollectionView, also in the sample project they are doing something similar to what you're trying to achieve, here is the link in case it helps: CollectionViewTutorial

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top