I have a variable width in my UICollectionView and in my function sizeForItemAtIndexPath, I end up returningCGSize
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
var width: CGFloat = indexPath.row == 0 ? 37 : 20
let font = UIFont.systemFont(ofSize: 14, weight: UIFontWeightRegular)
if indexPath.row == 0 {
width += listingsFilter.stringValue.width(withConstrainedHeight: 28, font: font)
} else {
let string = viewModel.valueRangeForButton[indexPath.row - 1]
width += string.width(withConstrainedHeight: 28, font: font)
}
print(width)
let w: CGFloat = width.rounded()
return CGSize(width: w, height: 28)
}
If I close the return value with a number for the width, say 128, and not with a variable, the collection view will look UIEdgeInsets. If I return CGSizewith a variable for the width, it UIEdgeInsetswill be ignored for each element, but the first and last.
I feel like I discovered some deep mistake CoreGraphics.
For the last two lines, if I change them to
let x: CGFloat = 128
return CGSize(width: x, height: 28)
It still does not work. I tried to return CGSizewith an initializer Int. I tried casting for Ints and returned to CGFloats. Nothing seems to work.
. width ( ) - .
. - ?
:
CGSize(width: w, height: 28), w CGFloat, 128 . CGSize(width: 128, height: 28)

