简体   繁体   English

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

[英]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. 我把我的手机的_containerWidthcellForItemAtIndexPath在我的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.

相关问题 如何以编程方式分配UIViews自动布局边缘以匹配超级视图? - How do I programatically assign a UIViews Autolayout edges to match the superview? 如何使我的超级视图调整大小,以使其子视图的大小与Autolayout相匹配? - How do I make my superview resize to match the size of its subviews with Autolayout? 如何在iOS中确定UIImageView的超级视图 - How do I determine the superview of UIImageView in iOS 如何找到最父级的SuperView? - How do I find the most parent SuperView? 如何在超视图中定位UIView? - How do I position UIView in superview? 如何检查 UIView 是否超出其超视图范围 - How to check if a UIView is out of its superview bounds 如果我从可重用单元格的超级视图中删除UILabel,那么在重用时如何在单元格中重新初始化UILabel? - If I remove a UILabel from superview in a Reusable cell, how can I reinitialise the UILabel in the cell when reusing? 如何指定间距约束作为超级视图尺寸的一部分? - How do I specify a spacing constraint as a proportion of the superview's size? 如何将子视图大小调整传播到superview大小调整? - How do I propagate subview resizing to superview resizing? 如何通过自动调整大小使UICollectionView填充其超级视图? - How do I make a UICollectionView fill it's superview with autosizing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM