Search code examples
iosobjective-cxcodeuicollectionviewuicollectionviewlayout

UICollectionViewCell Spacing is set to 0 but still have spacing


I am trying to make the spacing between my cells = 0

Example of desired result:

enter image description here

What I currently have (iPhone 6):

enter image description here

My code:

- (UICollectionViewFlowLayout *)setCollectionViewFlowLayout
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];

    //iphone5 100
    //iphone6 118
    layout.itemSize             = CGSizeMake(118, 118);
    layout.footerReferenceSize  = CGSizeMake(0, 0);

    layout.sectionInset            = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.minimumInteritemSpacing = 0.0;
    layout.minimumLineSpacing      = 0.0;

    return layout;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

What I have on IB:

enter image description here

What am I missing? Thanks in advance!


Solution

  • update your cellsize and imageview size if you didn't set. you have to set your imageview's constraint fit with cell.

    replace

    layout.itemSize = CGSizeMake(118, 118);
    

    with

    layout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width /3, [UIScreen mainScreen].bounds.size.width /3);
    

    I set screensize because i assume your collectionview is fit to screen width. replace it with your collectionview size.

    Because you are trying to show 3 cells but you set fix width for your item. so just update your cell size. Because in different screen your collectionview widht will be change so you should update your cell size also.