UICollectionView cell size in AutoLayout

So, I'm using a UICollectionView under the AutoLayout -enabled storyboard. I am trying to set the cell size based on collectionView and it is based on the [collectionView: layout: sizeForItemAtIndexPath:] method. collectionView also depends on the automatic layout, and it gives the wrong size for the first time (I assume this is before the layout is displayed). I know that they will have the correct size after calling the viewDidLayoutSubviews method, but it causes a double reload of the collectionView elements, which makes the user interface crash at runtime.

Here is the [collectionView: layout: sizeForItemAtIndexPath:] method of my implementation.

 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { CGSize innerSize = CGSizeMake(galleryView.frame.size.width - 80, galleryView.frame.size.height - 40); GLPhotoAsset *photo = [(PCOpticsPhotoPoint *)_cluster.points[indexPath.row] photo]; CGFloat ratio = MIN(innerSize.width / photo.size.width, innerSize.height / photo.size.height); return CGSizeMake(photo.size.width * ratio, photo.size.height * ratio); } 
+6
source share
2 answers

Verify that the Clip Subviews., View property is checked.

And your cellView property cellView should be correct in the section Size and location.,.

Maybe this will help you.,.

+1
source

As you get the wrong layout size before sizeForItemAtIndexPath is called: then go to the storyboard and check the cell size for these 3 - CollectionView , CollectionViewCell and CollectionViewFlowLayout

Make them the same. (also specify the size relative to the size of the default collection view, as you need)

0
source

Source: https://habr.com/ru/post/972083/


All Articles