I have the following setup:
UICollectionView with self sizing cells, where their height is defined by their content
UICollectionViewCompositionalLayout that defines the dimensions of items like this
let cellHeight: CGFloat = 260 let heightDimension = NSCollectionLayoutDimension.estimated(cellHeight) let fractionalWidth: CGFloat = 1.0 let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(fractionalWidth), heightDimension: heightDimension) let item = NSCollectionLayoutItem(layoutSize: itemSize) let groupWidth = NSCollectionLayoutDimension.fractionalWidth(1.0) let groupSize = NSCollectionLayoutSize(widthDimension: groupWidth, heightDimension: heightDimension) let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])
This works fine, until I want to update the height of one of the cells. I get the following warning:
[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)>
The original height of the cell is indeed 125
defined by the placeholder size, however when the update comes over the network I'm creating a height constraint on the cells contentView
like this:
cell.heightConstraint = cell.contentView.heightAnchor.constraint(equalToConstant: size.height)
cell.heightConstraint.identifier = "MyCell.contentView.heightAnchor"
cell.heightConstraint.isActive = true
snapshot?.reloadItems([cellIdentifier])
Where the size.height
is the new height. So, the question is, what am I missing here? I'm assuming NSAutoresizingMaskLayoutConstraint
is created by the Autolayout from the original height of the cell, but why is it not reset/removed when I add the height constraint to the cells contentView
?