UIColletoinView 水平滚动与项目间间距 [英] UICollectoinView horizontal scroll with inter item spacing

查看:18
本文介绍了UIColletoinView 水平滚动与项目间间距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

每个图像都应以屏幕大小显示,因此一个单元格具有屏幕宽度.flowLayout 的 minimumInterItemSpacing 为 25.所以现在的问题是,如果我滚动,集合视图不会滚动到下一个图像的开头,而是滚动到 interItemSpacing 的开头.

Each image should be displayed at the size of the screen, therefore one cell has the width of the screen. The minimumInterItemSpacing of the flowLayout is 25. So now the problem is, that if I am scrolling, the collection view doesn't scroll to the start of the next image, but to the start of the interItemSpacing.

举个例子:

Image/Cell width = 320
CollectionView's interItemSpacing = 25

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

If I scroll one page, the scroll view content offset is at 320 and not at 345 meaning that the second cell is not in the center of the screen.

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

How to solve this issue? Any suggestions?

推荐答案

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

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

<代码>1.UICollectionViewController 大小

通过添加您需要的值作为 interItemSpacing 来增加集合视图及其项目的大小.

Increasing the size of the collection view and it's items by adding exactly the value you need as 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];

}

<代码>2.SectionEdgeInset

制作一页 = 一个部分并使用 sectionEdgeInset 会产生相同的解决方案,但 - 可以肯定 - 并不总是一种选择!

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

这篇关于UIColletoinView 水平滚动与项目间间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆