首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UICollectionViewCompositionalLayout更新单元高度

UICollectionViewCompositionalLayout更新单元高度
EN

Stack Overflow用户
提问于 2020-09-24 10:26:19
回答 2查看 766关注 0票数 0

我有以下设置:

具有自调整大小单元格的

  • UICollectionView,其中其高度由其内容

定义。

定义如下项的维度的

  • UICollectionViewCompositionalLayout

设cellHeight: CGFloat = 260 let heightDimension = CGFloat let fractionalWidth: CGFloat= 1.0 let itemSize = NSCollectionLayoutSize(widthDimension:.fractionalWidth(fractionalWidth),heightDimension: heightDimension)让项目= NSCollectionLayoutItem(layoutSize: heightDimension)让CGFloat=.fractionalWidth=(:,:)让组=,分项:项目)

这很好,直到我想更新其中一个单元格的高度。我收到以下警告:

代码语言:javascript
复制
   [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. 
Try this: 
    (1) look at each constraint and try to figure out which you don't expect; 
    (2) find the code that added the unwanted constraint or constraints and fix it. 
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)

"<NSAutoresizingMaskLayoutConstraint:0x6000013bdfe0 h=--& v=--& UIView:0x7fd64017a040.height == 125   (active)>",
"<NSLayoutConstraint:0x6000013b3b60 'MyCell.contentView.heightAnchor' UIView:0x7fd64017a040.height == 231   (active)>"

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6000013b3b60 'MyCell.contentView.heightAnchor' UIView:0x7fd64017a040.height == 231   (active)>

单元格的原始高度确实是由占位符大小定义的125,但是当更新通过网络时,我将在单元格contentView上创建一个高度约束,如下所示:

代码语言:javascript
复制
cell.heightConstraint = cell.contentView.heightAnchor.constraint(equalToConstant: size.height)
cell.heightConstraint.identifier = "MyCell.contentView.heightAnchor"
cell.heightConstraint.isActive = true
snapshot?.reloadItems([cellIdentifier])

其中size.height是新的高度。所以,问题是,我在这里错过了什么?假设NSAutoresizingMaskLayoutConstraint是由单元格原始高度的Autolayout创建的,但是当我将高度约束添加到单元格contentView时,为什么不重置/删除它呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-10-01 14:37:49

我想了更多关于这一点,最后我一起取消了高度限制。我认为,如果我遵循自定尺寸的单元格哲学,那么我就不需要任何高度约束,而是应该由适当定义的单元格内容的约束来确定高度。

因此,我再次检查了我的单元格,发现了一个问题,就是有些内容是添加到单元格本身中的,而不是contentView。在修复并移除高度约束后,我更改了以下内容:

代码语言:javascript
复制
snapshot?.reloadItems([cellIdentifier])

这方面:

代码语言:javascript
复制
self?.collectionView.collectionViewLayout.invalidateLayout()

一切都开始正常运转了。

票数 2
EN

Stack Overflow用户

发布于 2020-09-26 03:14:37

我发现最好在一个单独的xib文件中设计这个单元。在该文件中,将最顶部的"Cell“元素的大小检查器>布局设置为”推断(约束)“。这将删除与设置的约束冲突的自动调整掩码。

如果这样做很好,那么布局可能是创建自动调整掩码本身,您可以使布局失效,以便它根据单元格所需的大小重新创建自动调整掩码。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64044481

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档