I have embedded a collection view in another view and disabled the collection view's scrolling ability, what i want to achieve is similar to Instagram's profile tab. However, I cannot figure out how should I set the height of the collection view in this case since the number of cells are dynamic.
I tried searching different solutions but most results are on changing the cells dynamically but not the collection view height itself. Is there any default/standard solutions for that?
In your ViewController, set the frame of the collectionView. For example:
Based on the fact you know the height of the cells and you know the number of cells on the screen. I would...
or have I miss understood the question?
For Swift please follow below steps:
Declare a
CGFloat
variable in declaration section:At
viewDidAppear
you can get it by:Maybe when you
reload
data then need to calculate a new height with new data then you can get it by:addObserver
to listen when yourCollectionView
finished reload data atviewWillAppear
:Then add bellow function to get new height or do anything after
collectionview
finished reload:And don't forget to remove observer:
I hope this will help you to solve your issue in swift.
Set the width you want the collection view to have (hopefully that is static), and request a layout:
The calculated height of the collectionView will then be available at: