2015-09-07 45 views
0

我有一個UICollectionViewController和一個UICollectionViewCell如何將單元格邊界與超視圖的邊界進行匹配?

我想從UICollectionViewCell的邊界到UICollectionViewController的界限有一個前導和尾隨空間,所以當方向改變時,我的單元格將調整到新的水平邊界。

這是怎麼做到的?

編輯:我使用estimatedItemSize來計算我的UICollectionViewFlowLayout中的尺寸(如果有幫助)。

+0

「UICollectionViewCell」和「UICollectionViewController」的高度/寬度是否相同? –

+0

我的意圖是做尾隨空間--20的寬度間距,只是擔心寬度,而不是單元格的高度。 – thecodingmate

回答

0

好的主要解決它。因爲我正在使用iOS 8的自動單元格大小調整(如estimatedItemSize),所以我需要在我的單元格中實現preferredLayoutAttributesFittingAttributes。這是我的實施:

- (UICollectionViewLayoutAttributes * _Nonnull)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes * _Nonnull)layoutAttributes { 
    UICollectionViewLayoutAttributes *attr = [layoutAttributes copy]; 
    CGRect newFrame = attr.frame; 
    self.frame = newFrame; 

    [self setNeedsLayout]; 
    [self layoutIfNeeded]; 

    CGFloat desiredHeight = [self.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height; 
    newFrame.size.height = desiredHeight; 
    newFrame.size.width = _containerWidth - 20; 
    attr.frame = newFrame; 
    return attr; 
} 

其中_containerWidth是我的細胞屬性。我在我的collectionview中將我的單元的_containerWidth設置爲cellForItemAtIndexPath

雖然可能有一個更優雅的方式來做到這一點,我只是在旋轉期間在collectionview上調用reloadData

我暫時放棄了爲屏幕旋轉調整單元格的大小,但這應該足以滿足此問題的需要。如果有人找到更好的解決方案,我會留下這個問題。