我有以下设置:
具有自调整大小单元格的
定义。
定义如下项的维度的
设cellHeight: CGFloat = 260 let heightDimension = CGFloat let fractionalWidth: CGFloat= 1.0 let itemSize = NSCollectionLayoutSize(widthDimension:.fractionalWidth(fractionalWidth),heightDimension: heightDimension)让项目= NSCollectionLayoutItem(layoutSize: heightDimension)让CGFloat=.fractionalWidth=(:,:)让组=,分项:项目)
这很好,直到我想更新其中一个单元格的高度。我收到以下警告:
[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上创建一个高度约束,如下所示:
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时,为什么不重置/删除它呢?
发布于 2020-10-01 14:37:49
我想了更多关于这一点,最后我一起取消了高度限制。我认为,如果我遵循自定尺寸的单元格哲学,那么我就不需要任何高度约束,而是应该由适当定义的单元格内容的约束来确定高度。
因此,我再次检查了我的单元格,发现了一个问题,就是有些内容是添加到单元格本身中的,而不是contentView。在修复并移除高度约束后,我更改了以下内容:
snapshot?.reloadItems([cellIdentifier])这方面:
self?.collectionView.collectionViewLayout.invalidateLayout()一切都开始正常运转了。
发布于 2020-09-26 03:14:37
我发现最好在一个单独的xib文件中设计这个单元。在该文件中,将最顶部的"Cell“元素的大小检查器>布局设置为”推断(约束)“。这将删除与设置的约束冲突的自动调整掩码。
如果这样做很好,那么布局可能是创建自动调整掩码本身,您可以使布局失效,以便它根据单元格所需的大小重新创建自动调整掩码。
https://stackoverflow.com/questions/64044481
复制相似问题