简体   繁体   English

AutoLayout中的UICollectionView单元格大小

[英]UICollectionView cell size in AutoLayout

So I'm using UICollectionView under AutoLayout -enabled storyboard. 所以我在UICollectionView AutoLayout storyboard下使用UICollectionView I'm trying to set cell size based on collectionView itself and it's based on [collectionView: layout: sizeForItemAtIndexPath:] method. 我正在尝试基于collectionView本身设置单元格大小,它基于[collectionView: layout: sizeForItemAtIndexPath:]方法。 collectionView also depends on auto layout and it gives wrong size at first time (I assume this is before view is layouted). collectionView还依赖于自动布局,并且第一次给出错误的大小(我假设这是在视图布局之前)。 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. 我知道他们有正确的尺寸之后viewDidLayoutSubviews方法被调用,但它造成的双重重载collectionView这使得UI毛刺在运行时项目。

Here is [collectionView: layout: sizeForItemAtIndexPath:] method of my implementation. 这是我实现的[collectionView: layout: sizeForItemAtIndexPath:]方法。

- (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 . 并且您的collectionView属性cellSize必须在Size&Location Section中正确。 . .

May be this helps you . 可能这对你有所帮助。 . .

As you are getting a wrong layout size before sizeForItemAtIndexPath: is called, then goto storyboard and check the cell size given for these 3 - CollectionView , CollectionViewCell and CollectionViewFlowLayout 当你在调用sizeForItemAtIndexPath:之前得到错误的布局大小时,然后转到storyboard并检查为这些3给出的单元格大小 - CollectionViewCollectionViewCellCollectionViewFlowLayout

Make them same . 让他们一样。 (also make the size relative to the default collection view size, as u require) (也可以根据需要调整相对于默认集合视图大小的大小)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM