UICollectionView - distance between cells?

怎甘沉沦 提交于 2019-11-30 04:34:53

This UICollectionViewFlowLayoutDelegate method will help you..

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

One more if you need more space to arrange your cells adjust the edgeInsets also

You can configure spacing between cells or rows by using following properties.
1) For setting space between rows.

[self.collectionView setMinimumLineSpacing:5];

2) For setting space between items/cells.

[self.collectionView setMinimumInteritemSpacing:5];

You can also configure it from InterfaceBuilder(IB). Just select UICollectionView from storyboard/Xib file and click in Size Inspector as specified in below image.

Hsm

From this. You need to change minimumInteritemSpacing and minimumLineSpacing.

UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
flow.itemSize = CGSizeMake(cellWidth, cellHeight);
flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
flow.minimumInteritemSpacing = 0;
flow.minimumLineSpacing = 0;
mainCollectionView.collectionViewLayout = flow;

No, it doesn't. Just change the layout's minimumInteritemSpacing.

Tanvi Jain

You can use delegate method like:- for changing the space between cells in a section.

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionView *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section  

For showing top,bottom y padding between cells of different sections.

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

// Layout: Set Edges //setting insets for cell

- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section 
//top,left,botttom,right

swift 3.0

 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {

    if DEVICE_IDIOM == .pad {
        return 15.0
    }
    else {
        return 10.0
    }
}

I had the same issue..

In my case I needed a retangular cell.

To achieve the desired result I used the UICollectionViewDelegateFlowLayout.

This is my implementation of one of the method:

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout*)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
{
    CGFloat width = CGRectGetWidth(self.view.frame)/3.0f;
    return CGSizeMake(width, (width * 1.1f));
}

This worked fine for me! I hope it helps someone.

Swift 4.2

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
   return 10
}

func collectionView(_ collectionView: UICollectionView, layout
            collectionViewLayout: UICollectionViewLayout,
                            minimumLineSpacingForSectionAt section: Int) -> CGFloat {

 return 10
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!