swift - Weird bug returning a CGSize in UICollectionView sizeForItemAtIndexPath -
i have variable widths in uicollectionview , in sizeforitematindexpath function, return cgsize
func collectionview(_ collectionview: uicollectionview, layout collectionviewlayout: uicollectionviewlayout, sizeforitemat indexpath: indexpath) -> cgsize { var width: cgfloat = indexpath.row == 0 ? 37 : 20 // default paddings based on spacing in cell 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) // decimal e.g. 138.1239581 let w: cgfloat = width.rounded() // rounded e.g. 13 return cgsize(width: w, height: 28) } if stub return value number width, 128, rather have variable, collection view respects uiedgeinsets. if return cgsize variable width, uiedgeinsets gets ignored every element first , last.
i feel found deep coregraphics bug.
for last 2 lines, if change them
let x: cgfloat = 128 // or 128.0, doesn't matter return cgsize(width: x, height: 28) it still doesn't work. i've tried returning cgsize int initializer. i've tried casting ints , cgfloats. nothing seems work.
i've narrowed down problem point. doesn't have width code above (which string extension) or else.
super weird. have experience this?
edit: images
top 1 cgsize(width: w, height: 28) w cgfloat equal 128 or whatever other value. bottom 1 cgsize(width: 128, height: 28)
i had similar issue few months ago. far remember annoying trying fix (as in case) ridiculously simple.
you need make sure class inherits uicollectionviewdelegateflowlayout. inherits uicollectionviewdelegate. should make trick.
let me know if works you.


Comments
Post a Comment