自动调整UICollectionView高度以调整其内容大小

问题描述 投票:22回答:4

我有一个UICollectionView,一个在集合视图中创建新单元格的按钮。我希望UICollectionView根据它的内容大小调整它的大小(当有一个或两个单元格时,那么UICollectionView很短,如果有很多单元格UICollectionView足够大)。

我知道如何获取内容大小:

collectionView.collectionViewLayout.collectionViewContentSize

但我不知道在哪里使用这个值。如果有人帮我弄清楚如何让UICollectionView自动调整它的高度,我将不胜感激。

UPD:我在GitHub上发布了一个描述问题的演示项目:https://github.com/avokin/PostViewer

ios uicollectionview
4个回答
6
投票

我不认为内容大小是你所追求的。我想你想要调整集合视图所消耗的屏幕空间量,对吧?这将需要调整框架。内容大小包括屏幕外(滚动)区域以及屏幕视图。

我不知道任何会阻止你动态改变帧大小的东西:

collectionView.frame = CGRectMake (x,y,w,h);
[collectionView reloadData];

如果我正确理解你。


6
投票

对集合视图使用高度约束,并在需要时使用内容高度更新其值。看到这个答案:https://stackoverflow.com/a/20829728/3414722


0
投票

更改UICollectionView框架的步骤:

  1. 对于collectioview的superview,将“translatesAutoresizingMaskIntoConstraints”属性设置为YES(如果使用的是AUTOLAYOUT)
  2. 然后将collectioview的框架更新为: collectionView.frame = CGRectMake (x,y,w,h); [collectionView reloadData];

0
投票

您需要将集合视图高度限制为内容的高度:

我在以下代码中使用SnapKit。

首先将集合视图边缘约束到其superview:

private func constrainViews() {
    collectionView?.translatesAutoresizingMaskIntoConstraints = true

    collectionView?.snp.makeConstraints { make in
        make.edges.equalToSuperview()
        heightConstraint = make.height.equalTo(0).constraint
    }
}

接下来计算高度并将高度设置为高度约束偏移量。我让流程布局完成工作,然后根据最后一个布局属性的下边缘计算高度:

override func viewDidLayoutSubviews() {
    guard
        let collectionView = collectionView,
        let layout = collectionViewLayout as? UICollectionViewFlowLayout
    else {
        return
    }

    let sectionInset = layout.sectionInset
    let contentInset = collectionView.contentInset

    let indexPath = IndexPath(item: tags.count, section: 0)
    guard let attr = collectionViewLayout.layoutAttributesForItem(at: indexPath) else {
        return
    }

    // Note sectionInset.top is already included in the frame's origin
    let totalHeight = attr.frame.origin.y + attr.frame.size.height
        + contentInset.top + contentInset.bottom
        + sectionInset.bottom

    heightConstraint?.update(offset: totalHeight)
}

请注意,在示例中,我的项目tags计数中始终没有包含一个特殊标记,因此该行:

let indexPath = IndexPath(item: tags.count, section: 0)

在大多数其他代码中需要像if items.count > 0 ... let indexPath = IndexPath(item: tags.count - 1, section: 0)这样的东西。

© www.soinside.com 2019 - 2024. All rights reserved.