UICollectoinView horizontal scroll with inter item spacing

后端 未结 3 1418
温柔的废话
温柔的废话 2021-01-05 18:19

I am using a collection view for some of my images.

Each image should be displayed at the size of the screen, therefore one cell has the width of the screen. The

3条回答
  •  长情又很酷
    2021-01-05 19:09

    I found a solution to it, and it involved subclassing the UICollectionViewFlowLayout.

    My CollectionViewCell size is 302 X 457 and i set my minimum line spacing to be 18 (9pix for each cell)

    When you extend from that class there are a few methods that need to be over-ridden. One of them is

    • (CGSize)collectionViewContentSize

    In this method, I needed to add up the total width of what was in the UICollectionView. That includes the ([datasource count] * widthOfCollectionViewCell) + ([datasource count] * 18)

    Here is my custom UICollectionViewFlowLayout methods....

    -(id)init
    {
        if((self = [super init])){
    
           self.itemSize = CGSizeMake(302, 457);
           self.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
           self.minimumInteritemSpacing = 0.0f;
           self.minimumLineSpacing = 18.0f;
           [self setScrollDirection:UICollectionViewScrollDirectionHorizontal];
       }
        return self;
    }
    
    
    
    -(CGSize)collectionViewContentSize{
       return CGSizeMake((numCellsCount * 302)+(numCellsCount * 18), 457);
    }
    

    This worked for me, so I hope someone else finds it useful!

提交回复
热议问题