[英]How do I match cell boundary to bounds of superview?
I have a UICollectionViewController
and a UICollectionViewCell
. 我有一个
UICollectionViewController
和一个UICollectionViewCell
。
I would like to have a leading and trailing space from the bounds of the UICollectionViewCell
to the bounds of the UICollectionViewController
so when the orientation changes my cells will resize to the new horizontal bounds. 我想有从的边界前缘和后空间
UICollectionViewCell
到的范围UICollectionViewController
所以当方向改变我的细胞将调整到新的水平范围。
How is this done usually? 通常如何做?
edit: I'm using estimatedItemSize
to calculate sizes in my UICollectionViewFlowLayout
if this helps. 编辑:我使用
estimatedItemSize
计算尺寸在我UICollectionViewFlowLayout
如果这有助于。
Ok mostly solved it. 好的,基本上解决了。 Because I'm using iOS 8's auto cell resizing (like
estimatedItemSize
) I need to implement preferredLayoutAttributesFittingAttributes
in my cell. 因为我使用的是iOS 8的自动单元格调整大小(如
estimatedItemSize
),所以我需要在单元格中实现preferredLayoutAttributesFittingAttributes
。 Here's my implementation: 这是我的实现:
- (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;
}
where _containerWidth
is a property in my cell. _containerWidth
是我单元格中的一个属性。 I set my cell's _containerWidth
in cellForItemAtIndexPath
in my collectionview. 我把我的手机的
_containerWidth
在cellForItemAtIndexPath
在我的CollectionView。
Although there's probably a more elegant way to do this, I just call reloadData
on the collectionview during rotation. 尽管可能有一种更优雅的方法,但我只是在旋转过程中在collectionview上调用
reloadData
。
I've temporarily given up on resizing cells for screen rotation as its so buggy, but this should suffice for the purposes of this question. 我暂时放弃了调整屏幕旋转单元格的大小,因为它有很多问题,但是对于这个问题,这已经足够了。 I'll leave this question open in case someone finds a better solution.
如果有人找到更好的解决方案,我将保留这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.