简体   繁体   中英

How can I calculate the size of a UICollectionViewCell loaded from a xib with autolayout aspect ratio constraints?

I have a UICollectionViewCell that is loaded from a xib and stored in a property to help with calculating cell sizes for my collection view. The collection view always has only one column, so the cells vary in their widths based on the phone the user has (iphone 5/6/6+). The xib has an image view with an aspect ratio that must remain constant for all phone sizes.

The problem I run into stems from this configuration. I cannot seem to make the cell size the way I want based on a fixed width. My sizeForItemAtIndexPath: method looks like this:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    self.sizingCell.frame.size.width = self.flowLayout.fullViewSectionSize.width;
    [self configureCell:self.sizingCell atIndexPath:indexPath];
    [self.sizingCell setNeedsLayout];
    [self.sizingCell layoutIfNeeded];
    CGSize size = [self.sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    return size;
}

I'm doing what I've read I'm supposed to for dynamic size calculations with an off-screen cell, but the problem is that when layoutIfNeeded is called, I get an unsatisfiable constraint exception with my constraints and an extra constraint that sets the height of the cell to be the height of it as defined in the xib. The system never breaks this extra height constraint I did not add, so the height of the cell is always this incorrect height of the view at design time in the xib.

Can I somehow remove this extraneous constraint that causes this to not work? Is there some other setting to set here that does what I need? I've tried turning off translatesAutoResizingMaskIntoConstraints to no avail as well. How can I make a xib-loaded cell figure out it's size based on constraints involving aspect ratios?

Edit:

If you want to see what i'm talking about in action, I put together a swift version of this in a github repo here: https://github.com/klundberg/Autolayout-CollectionViewExample

I figured this out with a little workaround.

Originally, my xib was a collection view cell whose subview was an image with the aspect ratio constraint in there. This conflicted with the default constraints that iOS adds to these cells (named "UIView-Encapsulated-Layout-Height/Width"). My solution was to introduce one level of view in between the cell and the views i cared about laying out, so now this is a cell that contains a container view which contains my image view. The container view does not get the encapsulated layout constraints applied to it, so if I make an IBOutlet for it and call systemLayoutSizeFittingSize: on that view (instead of the contentView after setting its width, it will correctly calculate the height with no constraint conflicts.

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