UICollectoinView horizontal scroll with inter item spacing

后端 未结 3 1352
温柔的废话
温柔的废话 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:04

    Okay, what I've found out is, that there are 2 options to achieve the proper scrolling.

    1. UICollectionViewController size

    Increasing the size of the collection view and it's items by adding exactly the value you need as interItemSpacing.

    Here is some code:

    - (void) setupCollectionView;
    {
        PSTCollectionViewFlowLayout *flowLayout = [[PSTCollectionViewFlowLayout alloc] init];
        CGSize itemSize = self.view.bounds.size;
        itemSize.width +=25;
        [flowLayout setItemSize:itemSize];
        [flowLayout setScrollDirection:PSTCollectionViewScrollDirectionHorizontal];
        flowLayout.minimumInteritemSpacing = 0.0f;
        flowLayout.minimumLineSpacing = 0.0f;
    
        self.collectionView = [[PSTCollectionView alloc] initWithFrame:self.view.bounds
                                              collectionViewLayout:flowLayout];
        [self.collectionView registerClass:[AMDetailImageCell class]
            forCellWithReuseIdentifier:AMDetailImageCellIdentifier];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        self.collectionView.pagingEnabled = YES;
        CGRect rectSize = self.view.bounds;
        rectSize.size.width +=25;
        self.collectionView.frame = rectSize;
        [self.view addSubview:self.collectionView];
    
        [self scrollToStartIndex];
    

    }

    2. SectionEdgeInset

    Making one page = one section and using sectionEdgeInset would result in the same solution but is - for sure - not always an option!

提交回复
热议问题