14

How to remove space between two cells in UiCollectionView? Min spacing is not working.

image

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView.tag==101)
    {
        return CGSizeMake((_collView.frame.size.width),(_collView.frame.size.height));

    }
    else
    {
       return CGSizeMake((_collView2.frame.size.width/3)-20,300);
    }
}

But this is not working.

rmaddy
  • 298,130
  • 40
  • 468
  • 517
kartik patel
  • 446
  • 1
  • 4
  • 14

6 Answers6

16

*enter image description here *

Set Min Spacing for cells and for Lines to Zero and replace your code by

- (CGSize)collectionView:(UICollectionView *)collectionView layout:       (UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:   (NSIndexPath *)indexPath
{
if (collectionView.tag==101)
{
    return CGSizeMake((_collView.frame.size.width),(_collView.frame.size.height));
}
else
{
   return CGSizeMake((_collView2.frame.size.width/3),300);
}
 }
Vikky
  • 736
  • 1
  • 5
  • 15
12

Use UICollectionViewFlowLayout for those purposes:

UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
flow.itemSize = CGSizeMake(cellWidth, cellHeight);
flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
flow.minimumInteritemSpacing = 0;
flow.minimumLineSpacing = 0;
Vasilii Muravev
  • 2,733
  • 14
  • 41
1

This happens because of how UICollectionviewFlowLayout works. You could provide a custom collectionview layout: Left Align Cells in UICollectionView

Or use a 3d party solution: https://github.com/mokagio/UICollectionViewLeftAlignedLayout

Nikita Gaidukov
  • 761
  • 4
  • 13
0

use this code, based on your design adjust the width and height.

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath: (NSIndexPath *)indexPath {

CGSize s = CGSizeMake([[UIScreen mainScreen] bounds].size.width/2-10,
[[UIScreen mainScreen] bounds].size.height/2.9-7);
return s;
 }

bellow code adjust the edges of collection view

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

Use both delegates and change the width and height

Siva Sankar
  • 523
  • 1
  • 4
  • 18
-2

A method called minimumInteritemSpacingForSectionAtIndex from UICollectionViewFlowLayoutDelegate could help you. The minimunIteritemSpace set the space beetween items horizontally, for example try this code:

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionView *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section  
{ 
  return 0; // This is the minimum inter item spacing, can be more
}

Also you can use the edgeInsets to change the space beetween items.

Alfredo Luco G
  • 760
  • 6
  • 14
-2
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{


return 10.0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{


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


// return UIEdgeInsetsMake(0,8,0,8);  // top, left, bottom, right
return UIEdgeInsetsMake(20,20,20,20);  // top, left, bottom, right
}
Dishant Rajput
  • 1,145
  • 1
  • 8
  • 18