简体   繁体   中英

How do I match cell boundary to bounds of superview?

I have a UICollectionViewController and a 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.

How is this done usually?

edit: I'm using estimatedItemSize to calculate sizes in my UICollectionViewFlowLayout if this helps.

Ok mostly solved it. Because I'm using iOS 8's auto cell resizing (like estimatedItemSize ) I need to implement preferredLayoutAttributesFittingAttributes in my cell. 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. I set my cell's _containerWidth in cellForItemAtIndexPath in my collectionview.

Although there's probably a more elegant way to do this, I just call reloadData on the collectionview during rotation.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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