iosswiftuicollectionviewuicollectionviewdelegateuicollectionviewdelegateflowlayout

UICollectionView layout behave weirdly on scrolling


I'm setting a collection View in order to display content as on instagram. instagram's grid

In order to set the layout I'm using some delegate methods of UIcollectionviewDelegateFlowLayout :

extension ProfileViewController : UICollectionViewDelegateFlowLayout{

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat{
        return 0
    }


    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat{
        return 0
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: (view.frame.width)/3, height: (view.frame.width)/3)
    }
}

When I open the project, the collectionView works 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 the cell layout I've set before doesn't hold and the images become ultra zoomed and one on top of each other, like this: messed Up collectionView 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>>.

Solution

  • I think your problem could be the same as the problem in this post.

    Try fixing your problem by setting the UICollectionView's estimatedSize to none.

    This is because in Xcode 11 Cells in a CollectionView can now self-size with Auto Layout constrained views in the canvas.