如何计算从具有自动布局纵横比约束的xib加载的UICollectionViewCell的大小?

问题描述:

我有一个从xib加载并存储在属性中的UICollectionViewCell,以帮助计算集合视图的像元大小.收集视图始终只有一列,因此,单元格的宽度会根据用户所拥有的电话(iphone 5/6/6 +)而有所不同. xib的图像视图的长宽比对于所有电话尺寸都必须保持恒定.

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.

我遇到的问题源于此配置.我似乎无法根据固定宽度来按所需方式设置像元大小.我的sizeForItemAtIndexPath:方法看起来像这样:

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;
}

我正在做我本来应该在屏幕外的单元格中进行动态尺寸计算的内容,但是问题是,当调用layoutIfNeeded时,我的约束和约束得到了一个无法满足的约束异常一个额外的约束,将单元的高度设置为xib中定义的单元的高度.系统从不打破我没有添加的额外高度限制,因此,单元的高度始终是在设计时在xib中视图的这种不正确高度.

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.

我可以以某种方式删除导致此方法不起作用的无关紧要的约束吗?还有其他需要我设置的设置吗?我尝试关闭translatesAutoResizingMaskIntoConstraints也无济于事.如何根据涉及宽高比的约束条件使装有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?

如果您想了解我在说什么,请在此处的github存储库中汇总以下内容: https://github.com/klundberg/Autolayout-CollectionViewExample

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.

最初,我的xib是一个集合视图单元,其子视图是其中具有长宽比约束的图像.这与iOS添加到这些单元格的默认约束(名为"UIView-Encapsulated-Layout-Height/Width")相冲突.我的解决方案是在单元格和我关心的布局视图之间引入一个级别的视图,所以现在这是一个包含容器视图的单元格,该容器视图包含我的图像视图.容器视图没有应用封装的布局约束,因此如果我为其创建一个IBOutlet并在该视图上调用systemLayoutSizeFittingSize:(而不是设置其宽度后的contentView,它将正确计算高度没有任何约束冲突.

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.