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);
}
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 . . .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With