-->

UICollectionView - distance between cells?

2019-02-03 03:34发布

问题:

I'm using a UICollectionView with the cell width of 67, right now I am using a flow layout.

I have 3 cells in a row with 30px space between cells. How can make that distance less, so that I can fit four cells in a row? Does this method have something to do with it?

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

回答1:

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



回答2:

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;


回答3:

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.



回答4:

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



回答5:

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


回答6:

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
    }
}


回答7:

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.