如何获取默认的UICollectionView单元格大小?

Luk*_*ski 16 objective-c uicollectionview

我正在实现UICollectionViewDelegateFlowLayoutcollectionView:layout: sizeForItemAtIndexPath:(NSIndexPath*)indexPath方法,并计算新的单元格大小,我必须访问默认的.

我的问题是:如何从我的代码中访问storyboard中定义的默认单元格大小?

Luk*_*ski 26

我在撰写这个问题时找到了答案,因此,只是为了与您分享,这里有一些示例代码:

- (CGSize) collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath*)indexPath {

    // Get the default cell size as defined in storyboard:
    CGSize defaultSize = [(UICollectionViewFlowLayout*)collectionViewLayout itemSize];

    if (something) {
        return defaultSize;
    }

    CGSize newSize = defaultSize;
    newSize.height *= 2;

    return newSize;
}
Run Code Online (Sandbox Code Playgroud)