![](/img/trans.png)
[英]How do I programatically assign a UIViews Autolayout edges to match the superview?
[英]How do I match cell boundary to bounds of superview?
我有一個UICollectionViewController
和一個UICollectionViewCell
。
我想有從的邊界前緣和后空間UICollectionViewCell
到的范圍UICollectionViewController
所以當方向改變我的細胞將調整到新的水平范圍。
通常如何做?
編輯:我使用estimatedItemSize
計算尺寸在我UICollectionViewFlowLayout
如果這有助於。
好的,基本上解決了。 因為我使用的是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
是我單元格中的一個屬性。 我把我的手機的_containerWidth
在cellForItemAtIndexPath
在我的CollectionView。
盡管可能有一種更優雅的方法,但我只是在旋轉過程中在collectionview上調用reloadData
。
我暫時放棄了調整屏幕旋轉單元格的大小,因為它有很多問題,但是對於這個問題,這已經足夠了。 如果有人找到更好的解決方案,我將保留這個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.