给定具有多行标签的视图单元格的宽度,如何获得首选的AutoLayout高度? [英] How to get preferred AutoLayout height given a width for a collection view cell with multiline labels?

查看:84
本文介绍了给定具有多行标签的视图单元格的宽度,如何获得首选的AutoLayout高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用UICollectionView在我的应用程序中构建具有自动调整大小的列表视图.使UICollectionViewFlowLayout进行垂直列表布局很麻烦,所以我正在编写自己的UICollectionViewLayout子类来实现它.

I'm building a list view with self-sizing in my app using UICollectionView. Getting UICollectionViewFlowLayout to do a vertical list layout is a pain, so I'm writing my own UICollectionViewLayout subclass to do it.

我的单元格看起来很像常规表格视图单元格-左侧是图像视图,中间垂直堆叠了几个标签,右侧是附件视图.标签可以换成几行,并根据系统字体大小设置增加字体大小,这就是为什么它们需要自动调整大小的原因.约束是明智的-从左到图像视图,从图像视图到标签,从标签到附件,从附件到右边,从标签到顶部和底部.

My cells look a lot like regular table view cells - an image view on the left, a couple of labels vertically stacked in the center, and an accessory view on the right. The labels can wrap to a few lines and grow in font size according to the system font size setting, which is why they need to be self-sizing. Constraints are sensible - left to image view, image view to label, label to accessory, accessory to right, label to top and bottom.

要通过 preferredLayoutAttributesFittingAttributes ,鉴于集合视图的宽度,我需要获得所需的单元格高度.我希望为此使用 systemLayoutSizeFittingSize:horizontalPriority:verticalPriority: ,大小,例如{desiredWidth, crazyLargeHeight}UILayoutPriorityRequired(水平优先级)和1(垂直优先级).但是我从systemLayoutFittingSize获得的大小并不合理.宽度是单元格中的某个先前宽度值(不一定与self.bounds.size.width或传入的layoutAttributes'size.width匹配),并且高度与该宽度匹配.我还尝试过传递一个较小的高度,而不是传递一个较大的高度,但是它仍然没有返回实际所需的大小.

To size my cells via preferredLayoutAttributesFittingAttributes, I need to get the desired height of the cell given the width of the collection view. I'd expect to use systemLayoutSizeFittingSize:horizontalPriority:verticalPriority: for this, passing a size like {desiredWidth, crazyLargeHeight}, UILayoutPriorityRequired for horizontal priority, and 1 for vertical priority. But the size I get back from systemLayoutFittingSize isn't sensible. The width is some previous width value from the cell (which doesn't necessarily match self.bounds.size.width or the passed-in layoutAttributes' size.width), and a height that works with that width. I also have tried passing a small height instead of a large one, and it still doesn't return the size it actually needs.

那么,如何在给定宽度值的情况下获得单元格的首选高度?

So how do I get the preferred height for the cell given a width value?

示例代码:

-(UICollectionViewLayoutAttributes *)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
  UICollectionViewLayoutAttributes *result = [super preferredLayoutAttributesFittingAttributes:layoutAttributes];
  CGSize exampleSize = CGSizeMake(layoutAttributes.size.width, 20);
  CGSize size = [self systemLayoutSizeFittingSize:exampleSize withHorizontalFittingPriority:UILayoutPriorityRequired verticalFittingPriority:1];
  result.size = size;
  return result;
}

推荐答案

如果您要求单元格的内容视图提供尺寸而不是单元格本身,则可以使用它.而且您必须提供小于所需高度的样本高度.

Turns out if you ask the cell's content view for a size instead of the cell itself, it works. And you have to give a sample height that is smaller than will be needed.

-(UICollectionViewLayoutAttributes *)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
  UICollectionViewLayoutAttributes *result = [super preferredLayoutAttributesFittingAttributes:layoutAttributes];
  CGSize exampleSize = CGSizeMake(layoutAttributes.size.width, 20); //magic number for example purposes...my cell will definitely be taller
  CGSize size = [self.contentView systemLayoutSizeFittingSize:exampleSize withHorizontalFittingPriority:UILayoutPriorityRequired verticalFittingPriority:1];
  result.size = size;
  return result;
}

我猜测内容视图未按我期望的方式固定在单元格的边界上.如果由于某种原因插入了内容视图,则此确切的实现可能会失败.但是解决此问题的关键是,将单元格内容固定到单元格的内容视图并不意味着该单元格本身将按照您的期望计算大小.

I'm guessing that the content view isn't pinned to the cell's bounds the way I expected. And this exact implementation will likely fall down if the content view is inset for some reason. But the key to solving this was the fact that having the cell contents pinned to the cell's content view does not mean that the cell itself will compute a size as you expect.

这篇关于给定具有多行标签的视图单元格的宽度,如何获得首选的AutoLayout高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆