Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView cell size in AutoLayout

So I'm using UICollectionView under AutoLayout-enabled storyboard. I'm trying to set cell size based on collectionView itself and it's based on [collectionView: layout: sizeForItemAtIndexPath:] method. collectionView also depends on auto layout and it gives wrong size at first time (I assume this is before view is layouted). I know they will have correct size after viewDidLayoutSubviews method is called but it causes double reloading of the collectionView items which makes UI glitches at run time.

Here is [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);
}
like image 943
Pei Avatar asked Jul 11 '14 13:07

Pei


1 Answers

Make sure that your Clip Subviews property of view is Checked . . . .

And your collectionView property cellSize must be proper in Size & Location Section . . .

May be this helps you . . .

like image 132
iHardikTrivedi Avatar answered Sep 21 '22 10:09

iHardikTrivedi