ios 在 UITableViewCell 上调用 `[UIView -systemLayoutSizeFittingSize:]` 总是失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17576005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:18:40  来源:igfitidea点击:

Calling `[UIView -systemLayoutSizeFittingSize:]` on a UITableViewCell always fails

ioscocoa-touchuitableviewinterface-builderautolayout

提问by Aaron Brager

I want to use auto-layout for UITableViewCells. These table cells have a dynamic height (depending on text length).

我想为 UITableViewCells 使用自动布局。这些表格单元格具有动态高度(取决于文本长度)。

I'm using [UIView -systemLayoutSizeFittingSize:]to calculate the appropriate cell height (to return in [UITableView -heightForRowAtIndexPath:]) but I keep getting the following results:

我正在使用[UIView -systemLayoutSizeFittingSize:]计算适当的单元格高度(返回[UITableView -heightForRowAtIndexPath:]),但我不断得到以下结果:

  • If I pass UILayoutFittingCompressedSize, I get back a CGSize of (0,0).

  • If I pass UILayoutFittingExpandedSize, my app crashes with this error:

    *** Assertion failure in -[NSISLinearExpression incrementConstant:], /SourceCache/Foundation_Sim/Foundation-1043.1/Layout.subproj/IncrementalSimplex/NSISLinearExpression.m:620

  • 如果我通过UILayoutFittingCompressedSize,我会得到 (0,0) 的 CGSize。

  • 如果我通过UILayoutFittingExpandedSize,我的应用程序会因以下错误而崩溃:

    *** 断言失败 -[NSISLinearExpression incrementConstant:], /SourceCache/Foundation_Sim/Foundation-1043.1/Layout.subproj/IncrementalSimplex/NSISLinearExpression.m:620

(My guess is that this means some number is infinite.)

(我的猜测是这意味着某些数字是无限的。)

My implementation is simple. I calculate the height for each object, and then cache it:

我的实现很简单。我计算每个对象的高度,然后缓存它:

MessageCell *cell = // allocate a new cell...

//  set up the cell (assign text, images, etc)

CGSize size = [cell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];

self.cellHeight = size.height;  // size always equals (0, 0)

I hypothesize that this is a problem with the constraints I set, but:

我假设这是我设置的约束的问题,但是:

  • If I manually set cellHeightto a large value, the cells all look fine except the height is wrong.
  • Interface Builder gives me no warnings about ambiguous restraints
  • [cell hasAmbiguousLayout]returns NO.
  • My cell has, among other things, an image set at 48x48, so a size of (0, 0) shouldn't satisfy all the constraints.
  • 如果我手动设置cellHeight为一个大值,除了高度错误之外,单元格看起来都很好。
  • Interface Builder 没有给我关于不明确限制的警告
  • [cell hasAmbiguousLayout]返回NO
  • 我的单元格有一个设置为 48x48 的图像,因此 (0, 0) 的大小不应满足所有约束。

Any ideas?

有任何想法吗?

回答by TomSwift

This is what works for me (note 'contentView')

这对我有用(注意“contentView”)

CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];

EDIT: I answered a similar question and provided a complete example, here:

编辑:我回答了一个类似的问题并提供了一个完整的例子,在这里:

How to resize superview to fit all subviews with autolayout?

如何调整超级视图的大小以适应所有具有自动布局的子视图?

回答by Rafa? Augustyniak

It is hard to say something concrete basing on your post because you didn't post constraints that you use. Apple Documentation:

很难根据您的帖子说具体的内容,因为您没有发布您使用的约束。苹果文档:

systemLayoutSizeFittingSize:

Returns the size of the view that satisfies the constraints it holds.

systemLayoutSizeFittingSize:

返回满足它所拥有的约束的视图的大小。

Maybe you created constraints that can be interpreted in the way the size of the cell is equal to (0,0).

也许您创建了可以解释为单元格大小等于 (0,0) 的约束。

There is another way you can check the height of the cell with the text. You can put your text in the UITextView and then:

还有另一种方法可以检查带有文本的单元格的高度。您可以将文本放在 UITextView 中,然后:

UITextView textView; 
textView.text = @"l";
textView.font = [UIFont fontWithName:FONT_NAME size:FONT_SIZE];
//some code here
//
CGFloat cellHeight = textView.contentSize.height;

It is important to set the text and font (and every other property that can cause the change of the height of the UITextView) of the UITextView before using contentSize property. Also you must first add UITextView to the view.

在使用 contentSize 属性之前设置 UITextView 的文本和字体(以及可能导致 UITextView 高度变化的所有其他属性)很重要。此外,您必须首先将 UITextView 添加到视图中。

////// EDIT

////// 编辑

The problem with your approach with using constraints can be that you want to measure the cell which ISN'T added to the view so the system don't have all the informations it needs. It doesn't know how much space that will be for the space etc because it doesn't know the surrounding area of the cell

您使用约束的方法的问题可能是您想要测量未添加到视图中的单元格,因此系统没有它需要的所有信息。它不知道空间等会有多少空间,因为它不知道单元格的周围区域