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

时间:2013-06-09 17:42:34

标签: ios uicollectionview

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

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

collectionView.collectionViewLayout.collectionViewContentSize

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

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

4 个答案:

答案 0 :(得分:6)

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

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

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

如果我正确理解你。

答案 1 :(得分:6)

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

答案 2 :(得分:0)

更改UICollectionView框架的步骤:

  1. 为collectioview的superview设置“translatesAutoresizingMaskIntoConstraints”属性为YES(如果您使用的是AUTOLAYOUT)

  2. 然后将collectioview的框架更新为:

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

答案 3 :(得分:0)

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

我在以下代码中使用SnapKit。

首先将集合视图的边限制为其超级视图:

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)

相关问题