iOS 10中的collectionViewContentSize使用自定义单元格 [英] collectionViewContentSize in iOS 10 using self-sizing cells

查看:2863
本文介绍了iOS 10中的collectionViewContentSize使用自定义单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iOS 10之前,我有一个自我调整表视图,它只包含一个UICollectionView,其中包含使用标准UICollectionViewFlowLayout的自调整单元格。使用自动布局调整集合视图单元格的大小。为了使表格单元格本身正确,我必须找到集合视图的内容大小,并在表格单元格的systemLayoutSizeFittingSize中使用:withHorizo​​ntalFittingPriority:verticalFittingPriority:。

Prior to iOS 10, I had a self-sizing table view that solely consisted of a UICollectionView with self-sizing cells using a standard UICollectionViewFlowLayout. The collection view cells are sized using auto-layout. In order for the table cell to size itself correctly, I had to find the collection view's content size and use that within the table cell's systemLayoutSizeFittingSize:withHorizontalFittingPriority:verticalFittingPriority:.

I还发现对collectionView.collectionViewLayout.collectionViewContentSize的调用使用了estimatedItemSize而不是正确大小的单元格大小,除非我调用了collectionView layoutIfNeeded。这导致systemLayoutSizeFittingSize:

I also found that the call to collectionView.collectionViewLayout.collectionViewContentSize used the estimatedItemSize instead of the correctly sized cell sizes unless I called collectionView layoutIfNeeded. This resulted in a systemLayoutSizeFittingSize of:

- (CGSize) systemLayoutSizeFittingSize:(CGSize)targetSize withHorizontalFittingPriority:(UILayoutPriority)horizontalFittingPriority verticalFittingPriority:(UILayoutPriority)verticalFittingPriority
{
    self.collectionView.frame = CGRectMake(0, 0, targetSize.width, FLT_MAX);
    [self.collectionView layoutIfNeeded];

    CGSize collectionViewContentSize = self.collectionView.collectionViewLayout.collectionViewContentSize;
    CGFloat verticalPadding = fabs(self.collectionViewTopPaddingConstraint.constant) + fabs(self.collectionViewBottomPaddingConstraint.constant);
    CGSize cellSize = CGSizeMake(collectionViewContentSize.width, collectionViewContentSize.height + verticalPadding);

    return cellSize;
}

现在调用layoutIfNeeded导致 ***断言失败 - [_ UIFlowLayoutSection computeLayoutInRect:forSection:invalidating:invalidationContext:],/ BuildRoot / Library / Cache / com.apple.xbs / Source / UIKit_Sim / UIKit-3599.6 / UIFlowLayoutSupport.m:823

The call to layoutIfNeeded now causes a *** Assertion failure in -[_UIFlowLayoutSection computeLayoutInRect:forSection:invalidating:invalidationContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3599.6/UIFlowLayoutSupport.m:823

我是否通过在systemLayoutSizeFittingSize中调用layoutIfNeeded来违反某些道德规则?在使用自定义单元格时,是否有更好的方法来计算集合视图的内容大小?我宁愿不必从自动布局转到在代码中进行这些大小计算,但这肯定是最坏的选择。

Am I violating some ethical rule by calling layoutIfNeeded within systemLayoutSizeFittingSize? Is there some better method to calculate a collection view's content size when it is using self-sizing cells? I would rather not have to move from auto layout to doing these size calculations in code, but that is certainly a worst case option.

推荐答案

<这是iOS10中仅有iPhone Plus设备的一些奇怪错误。我遇到了同样的问题,我的解决方案就是这样调用 layoutIfNeeded

func numberOfSections(in collectionView: UICollectionView) -> Int {
    collectionView.layoutIfNeeded() // Patch: only to solve UIKit crash on Plus models
    return 1
}

在不同的UICollectionViewDataSources方法中做同样的事情也可以。

Doing the same thing in different UICollectionViewDataSources methods will work as well

这篇关于iOS 10中的collectionViewContentSize使用自定义单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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