简体   繁体   English

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

[英]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. 我有一个从xib加载并存储在属性中的UICollectionViewCell,以帮助计算集合视图的单元格大小。 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+). 收集视图始终只有一列,因此单元格的宽度根据用户使用的电话(iphone 5/6/6 +)而变化。 The xib has an image view with an aspect ratio that must remain constant for all phone sizes. xib的图像视图的纵横比对于所有电话尺寸都必须保持恒定。

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: 我的sizeForItemAtIndexPath:方法看起来像这样:

- (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. 我正在做我本来应该在屏幕外的单元格进行动态尺寸计算的内容,但是问题是,当调用layoutIfNeeded时,我的约束得到了一个无法满足的约束异常,并且设置了一个额外的约束单元格的高度为其在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. 系统从不打破我没有添加的额外高度限制,因此,单元的高度始终是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. 我尝试过关闭translatesAutoResizingMaskIntoConstraints也无济于事。 How can I make a xib-loaded cell figure out it's size based on constraints involving aspect ratios? 如何根据涉及宽高比的约束条件使装有xib的单元格确定其大小?

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 如果您想了解我在说什么,请在此处的github存储库中汇总以下内容: 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. 最初,我的xib是一个集合视图单元,其子视图是其中具有长宽比约束的图像。 This conflicted with the default constraints that iOS adds to these cells (named "UIView-Encapsulated-Layout-Height/Width"). 这与iOS添加到这些单元格的默认约束(名为“ 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. 容器视图没有应用封装的布局约束,因此如果我为其创建IBOutlet并在该视图上调用systemLayoutSizeFittingSize:而不是设置其宽度后的contentView ,它将正确地计算高度而没有约束冲突。

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

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