CollectionView layout gets mad after scrolling

Hi, I'm setting a collection View in order to display content as instagram Profile view with all the photos in the grid.

in order to set the layout I'm using some delegate methods of UIcollectionviewDelegateFlowLayout . When you open the project, the collectionView works well with the grid set well. The problem occurs when you scroll down very fast the collectionView so that you make momentaneously disappear all the cells. In that exact moment all the cell layout I've set before doesn't hold and the images become ultra zoomed and one on top of each other.

I don't know how to fix this issue.

Also in the compiler I get this error :


the item width must be less than the width of the UICollectionView minus the section insets left and right values, minus the content insets left and right values.

Please check the values returned by the delegate.

The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout: 0x7fbbe972d1f0>, and it is attached to <UICollectionView: 0x7fbbeb056400; frame = (0 0; 414 896); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0x60000203c4e0>; layer = <CALayer: 0x600002ef19c0>; contentOffset: {0, 424}; contentSize: {414, 16048}; adjustedContentInset: {88, 0, 83, 0}; layout: <UICollectionViewFlowLayout: 0x7fbbe972d1f0>; dataSource: <InstagramClone.ProfileViewController: 0x7fbbe9708890>>.

Answered by guala in 396964022

Actually I discovered that with Swift 4 UICollectionViewCells have difficulties in autoresizing themselves, so in this case you have to go in the size inspector and set Estimated Size to "none". After doing this everything works correctly

Please show the code of all the collection view delegate functions.


Hard to say without.


However, have a look here

https://stackoverflow.com/questions/32082726/the-behavior-of-the-uicollectionviewflowlayout-is-not-defined-because-the-cell


and try overriding the

shouldInvalidateLayoutForBoundsChange
function to return true


Try also adding:

flowLayout.estimatedItemSize = CGSize(width: 1, height: 1)

Thanks so much for adding the suggestions!

Does it work now ?


If so, great. And don't forget to close the thread.

Accepted Answer

Actually I discovered that with Swift 4 UICollectionViewCells have difficulties in autoresizing themselves, so in this case you have to go in the size inspector and set Estimated Size to "none". After doing this everything works correctly

Thank you!!

Thanks!

CollectionView layout gets mad after scrolling
 
 
Q