Cell spacing in UICollectionView

后端 未结 26 3626
旧巷少年郎
旧巷少年郎 2020-11-22 15:53

How do I set cell spacing in a section of UICollectionView? I know there is a property minimumInteritemSpacing I have set it to 5.0 still the spaci

26条回答
  •  深忆病人
    2020-11-22 16:31

    Supporting the initial question. I tried to get the spacing to 5px on the UICollectionView but this does not work, as well with a UIEdgeInsetsMake(0,0,0,0)...

    On a UITableView I can do this by directly specifying the x,y coordinates in a row...

    enter image description here

    Heres my UICollectionView code:

    #pragma mark collection view cell layout / size
    - (CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
        return [self getCellSize:indexPath];  // will be w120xh100 or w190x100
        // if the width is higher, only one image will be shown in a line
    }
    
    #pragma mark collection view cell paddings
    - (UIEdgeInsets)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
        return UIEdgeInsetsMake(0, 0, 0, 0); // top, left, bottom, right
    }
    
    - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    
        return 5.0;
    }
    

    Update: Solved my problem, with the following code.

    enter image description here

    ViewController.m

    #import "ViewController.h"
    #import "MagazineCell.h" // created just the default class. 
    
    static NSString * const cellID = @"cellID";
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    #pragma mark - Collection view
    -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
    {
        return 1;
    }
    -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
    {
        return 30;
    }
    
    -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        MagazineCell *mCell = (MagazineCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    
        mCell.backgroundColor = [UIColor lightGrayColor];
    
        return mCell;
    }
    
    #pragma mark Collection view layout things
    // Layout: Set cell size
    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
        NSLog(@"SETTING SIZE FOR ITEM AT INDEX %d", indexPath.row);
        CGSize mElementSize = CGSizeMake(104, 104);
        return mElementSize;
    }
    - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
        return 2.0;
    }
    
    - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
        return 2.0;
    }
    
    // Layout: Set Edges
    - (UIEdgeInsets)collectionView:
    (UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
       // return UIEdgeInsetsMake(0,8,0,8);  // top, left, bottom, right
        return UIEdgeInsetsMake(0,0,0,0);  // top, left, bottom, right
    }
    
    @end
    

提交回复
热议问题