Anyone had any luck subclassing UICollectionView? ...
# kotlin-native
c
Anyone had any luck subclassing UICollectionView? I’m having trouble making it have the height of its content size. Already tried so much stuff that I don’t really know what I could do more.
s
If you’re using fixed sized cells it isn’t too hard. Just calculate the height based on the number of items that would be displayed vertically, the line spacing between items and the section insets. Then use a height constraint on the collection view and set its constant value to your calculated value. If you’re including it in a scroll view, you’ll want to turn off scrolling in the collection view for the direction the scroll view scrolls. That said, if you’re putting it in a scroll view and the collection view is laying out vertically, you’re going to negate a lot of the benefit of using a collection view. The collection view will keep every cell in memory rather than a small subset that it reuses as the user scrolls. It would be better if your collection view was the root view and it contained multiple distinct sections. iOS 13 added some nice features to make this better. Sadly it is 13+ and can’t be used if you’re targeting lower versions of iOS.
c
@Sam thanks for your response. What I’m having trouble is the UICollectionView height itself, If i give it a fixed height using a constraint, it works fine, but what I’d like to do is it having its content size instead, but everytime I try to access its content size or its collectionViewLayout size, its always 0. Any idea?
s
Calculate what the height should be based on the data you’re feeding it and update that fixed height constraint every time you change the data.