I've defined my flow layout as follows
internal lazy var layout: UICollectionViewFlowLayout = {
let layout = UICollectionViewFlowLayout()
layout.minimumLineSpacing = 16
layout.sectionInset = UIEdgeInsets(top: layout.minimumLineSpacing, left: layout.minimumLineSpacing , bottom: layout.minimumLineSpacing , right: layout.minimumLineSpacing )
layout.estimatedItemSize = CGSize(width: UIScreen.main.bounds.size.width - (layout.sectionInset.left + layout.sectionInset.right), height: 10)
return layout
}()
In the cell then I set the height for the expected content
override open func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
setNeedsLayout()
layoutIfNeeded()
let size = contentView.systemLayoutSizeFitting(layoutAttributes.size)
var newFrame = layoutAttributes.frame
newFrame.size.height = ceil(size.height)
layoutAttributes.frame = newFrame
return layoutAttributes
}
And it works. I get the cells in the height that I expect them to be, but there is a nasty warning in the console coming up for each cell
[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.
(
"<NSLayoutConstraint:0x600003c1e490 V:|-(0)-[UIView:0x7fdc8bead5d0] (active, names: '|':VLCContentCollectionViewCell:0x7fdc8becf8e0 )>",
"<NSLayoutConstraint:0x600003c1ec60 V:[UIView:0x7fdc8bead5d0]-(0)-| (active, names: '|':VLCContentCollectionViewCell:0x7fdc8becf8e0 )>",
"<NSLayoutConstraint:0x600003c4bb60 V:|-(0)-[VLAsyncImageView:0x7fdc8bea20a0] (active, names: '|':UIView:0x7fdc8bead5d0 )>",
"<NSLayoutConstraint:0x600003c43070 V:[VLAsyncImageView:0x7fdc8bea20a0]-(0)-| (active, names: '|':UIView:0x7fdc8bead5d0 )>",
"<NSLayoutConstraint:0x600003c5f340 VLAsyncImageView:0x7fdc8bea20a0.height == 100 (active)>",
"<NSLayoutConstraint:0x600003f8cb90 'UIView-Encapsulated-Layout-Height' VLCContentCollectionViewCell:0x7fdc8becf8e0.height == 10 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600003c5f340 VLAsyncImageView:0x7fdc8bea20a0.height == 100 (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
Im pretty sure that it is because of this constraint 'UIView-Encapsulated-Layout-Height'
which holds the value for the expected item size of the flow layout, because if I set that to 100 the log message doesn't show up.
However, I'm looking for dynamic cell height, so even if I set it to 100 it will fail at other places.
So my question is if I'm doing something wrong in defining my dynamic cells? Or is there maybe a way to reduce the priority of the estimated cell height, so that it will brake that constraint in case?