在UICollectionView中显示图像 - 如何在图像之间实现固定的垂直间距?

Jon*_*nas 10 ios ios6 uicollectionview uicollectionviewlayout

我正在使用UICollectionView在iPhone应用程序(iOS6)中呈现图像网格.

我正在使用UICollectionView的垂直滚动,并且图像都具有固定的宽度和不同的高度.设置图像的宽度,以便在iPhone上显示3列图像.这工作正常,我得到了网格视图中显示的图像.

但是,由于我的图像高度不同,因此列中图像之间的垂直间距会有所不同,这看起来不太好,如下图所示(用HTML制作的模型):

该图像显示了当前图像的布局方式

相反,我希望实现更流畅的流动,其中列中图像之间的垂直间距是相同的.以下模型显示了我希望它如何工作:

我希望图像流如何工作的示例

有关如何解决这个问题的任何想法?

另外,作为一个额外的问题,如果应用程序不是为iOS6构建的,那么有没有人知道解决相同问题的方法(因为UICollectionView仅在iOS6中可用).通过使用第三方组件或使用标准iOS控件解决它.

Rob*_* R. 9

子类UICollectionViewFlowLayout并在该类中添加这些方法.(注意这是假定一个垂直方向,它跳过第一行,它们之间是一个常数10个像素.请参阅:如果需要水平版本,如何确定UICollectionView flowLayout中单元格之间的间距

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
NSArray* arr = [super layoutAttributesForElementsInRect:rect];
for (UICollectionViewLayoutAttributes* atts in arr) {
    if (nil == atts.representedElementKind) {
        NSIndexPath* ip = atts.indexPath;
        atts.frame = [self layoutAttributesForItemAtIndexPath:ip].frame;
    }
}
return arr;
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewLayoutAttributes* atts =
[super layoutAttributesForItemAtIndexPath:indexPath];

if (indexPath.item == 0 || indexPath.item == 1) // degenerate case 1, first item of section
    return atts;

NSIndexPath* ipPrev =
[NSIndexPath indexPathForItem:indexPath.item-2 inSection:indexPath.section];

CGRect fPrev = [self layoutAttributesForItemAtIndexPath:ipPrev].frame;
CGFloat rightPrev = fPrev.origin.y + fPrev.size.height + 10;
if (atts.frame.origin.y <= rightPrev) // degenerate case 2, first item of line
    return atts;

CGRect f = atts.frame;
f.origin.y = rightPrev;
atts.frame = f;
return atts;
}
Run Code Online (Sandbox Code Playgroud)

  • @Satyamsvv在ViewController类(不是布局子类)中使用UICollectionViewFlowLayout的委托方法 - (CGSize)collectionView:(UICollectionView*)collectionView布局:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath*)indexPath (2认同)

小智 5

对于 Swift 3,这段代码对我有用,包括第一行到顶部的空间......

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
    let arr = super.layoutAttributesForElements(in: rect)

    for atts:UICollectionViewLayoutAttributes in arr! {
        if nil == atts.representedElementKind {
            let ip = atts.indexPath
            atts.frame = (self.layoutAttributesForItem(at: ip)?.frame)!
        }
    }
    return arr
}

override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
    let atts = super.layoutAttributesForItem(at: indexPath)

    if indexPath.item == 0 || indexPath.item == 1 {
        var frame = atts?.frame;
        frame?.origin.y = sectionInset.top;
        atts?.frame = frame!;
        return atts
    }

    let ipPrev = IndexPath(item: indexPath.item - 2, section: indexPath.section)

    let fPrev = self.layoutAttributesForItem(at: ipPrev)?.frame

    let rightPrev = (fPrev?.origin.y)! + (fPrev?.size.height)! + 10

    if (atts?.frame.origin.y)! <= rightPrev {
        return atts
    }

    var f = atts?.frame
    f?.origin.y = rightPrev
    atts?.frame = f!
    return atts
}
Run Code Online (Sandbox Code Playgroud)