UICollectoinView具有项间距的水平滚动

Ale*_*der 5 iphone ipad ios uicollectionview

我正在使用我的一些图像的集合视图.

每个图像应以屏幕大小显示,因此一个单元格具有屏幕宽度.在minimumInterItemSpacing该FlowLayout中的是25.所以,现在的问题是,如果我滚动,收集视图不滚动到下一个图像的开始,但对interItemSpacing的开始.

我们举个例子:

Image/Cell width = 320
CollectionView's interItemSpacing = 25
Run Code Online (Sandbox Code Playgroud)

如果我滚动一页,则滚动视图内容偏移为320而不是345意味着第二个单元不在屏幕的中心.

如何解决这个问题?有什么建议?

Ale*_*der 2

好吧,我发现有两个选项可以实现正确的滚动。

1. UICollectionViewController size

通过准确添加所需的值作为 interItemSpacing 来增加集合视图及其项目的大小。

这是一些代码:

- (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];
Run Code Online (Sandbox Code Playgroud)

}

2. SectionEdgeInset

使一页=一个部分并使用sectionEdgeInset会产生相同的解决方案,但-当然-并不总是一种选择!