繁体   English   中英

如何将单元格边界与超级视图的边界匹配?

[英]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是我单元格中的一个属性。 我把我的手机的_containerWidthcellForItemAtIndexPath在我的CollectionView。

尽管可能有一种更优雅的方法,但我只是在旋转过程中在collectionview上调用reloadData

我暂时放弃了调整屏幕旋转单元格的大小,因为它有很多问题,但是对于这个问题,这已经足够了。 如果有人找到更好的解决方案,我将保留这个问题。

暂无
暂无

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

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