具有自动调整单元大小的 UICollectionView 错误地定位了补充视图 [英] UICollectionView with autosizing cells incorrectly positions supplementary view

查看:23
本文介绍了具有自动调整单元大小的 UICollectionView 错误地定位了补充视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有 Flow Layout 的 UICollectionView,并试图让 collectionView 根据 AutoLayout 约束适当地调整单元格的大小.

I am using a UICollectionView with a Flow Layout and trying to get the collectionView to size the cells appropriately according to AutoLayout constraints.

虽然单元格按预期工作,但我遇到了添加到 CollectionView 的任何补充视图的布局问题.

While the cells work as intended, I am running in to issues with the layout of any supplementary views that I add to the CollectionView.

具体来说,在我滚动后更正"之前,supplementaryView 将在初始布局上处于错误位置(即 y 原点不正确).

Specifically, the supplementaryView will be in the wrong position (i.e., the y origin is incorrect) on initial layout, before 'correcting' itself after I scroll.

作为参考,以下是我配置单元格大小的方式:

For reference, here is how I am configuring my cell sizing:

1.设置 collectionViewLayout 的预估项大小

let collectionView: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    layout.estimatedItemSize = CGSizeMake(375, 50.0)
    layout.minimumInteritemSpacing = 0.0
    layout.minimumLineSpacing = 0.0
    let view = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
    view.backgroundColor = UIColor.whiteColor()
    view.alwaysBounceVertical = true
    return view
}()

<强>2.使用 AutoLayoutCollectionViewCell 的子类

class AutoLayoutCollectionViewCell: UICollectionViewCell {
    override func preferredLayoutAttributesFittingAttributes(layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
        layoutIfNeeded()
        layoutAttributes.bounds.size.height = systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height
        return layoutAttributes
    }
}

请注意,此时,一切都按预期进行.

Note that at this point, everything works as intended.

下一步是我们失败的地方.

The next step is where we fail.

3.为标题提供参考尺寸

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSizeMake(CGRectGetWidth(collectionView.frame), 30.0) 

}

我的问题是:为什么会这样?我怎样才能纠正这个问题?我应该如何处理可以自行调整单元格大小的 collectionView 中的补充视图?

My question is: Why does this happen? How can I get this to correct? How am I supposed to handle supplementary views within a collectionView that self-sizes its cells??

推荐答案

我遇到了同样的问题,为我解决的方法是继承 UICollectionViewFlowLayout 并覆盖以下函数:

I had the same problem, what solved it for me was to subclass UICollectionViewFlowLayout and override the following function:

override func invalidationContext(forPreferredLayoutAttributes preferredAttributes: UICollectionViewLayoutAttributes, withOriginalAttributes originalAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutInvalidationContext {
    let context = super.invalidationContext(forPreferredLayoutAttributes: preferredAttributes, withOriginalAttributes: originalAttributes)
    context.invalidateSupplementaryElements(ofKind: UICollectionElementKindSectionHeader,
                                                at: [originalAttributes.indexPath])
    return context
}

这篇关于具有自动调整单元大小的 UICollectionView 错误地定位了补充视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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