UICollectionView - distance between cells?

2019-02-03 03:19发布

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

7条回答
Emotional °昔
2楼-- · 2019-02-03 04:02

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

查看更多
\"骚年 ilove
3楼-- · 2019-02-03 04:02

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

查看更多
Lonely孤独者°
4楼-- · 2019-02-03 04:05

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
查看更多
ら.Afraid
5楼-- · 2019-02-03 04:07

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. enter image description here

查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-02-03 04:10

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楼-- · 2019-02-03 04:13

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.

查看更多
登录 后发表回答