NSAutoresizingMaskLayoutConstraint 的 UITableViewCell 舍入错误,但在 Storyboard 和 heightForRowAtIndexPath 中正确设置了大小:

UITableViewCell rounding error with NSAutoresizingMaskLayoutConstraint, but size correctly set in Storyboard AND heightForRowAtIndexPath:(NSAutoresizingMaskLayoutConstraint 的 UITableViewCell 舍入错误,但在 Storyboard 和 heightForRowAtIndexPath 中正确设置了大小:) - IT屋-程序

本文介绍了NSAutoresizingMaskLayoutConstraint 的 UITableViewCell 舍入错误,但在 Storyboard 和 heightForRowAtIndexPath 中正确设置了大小:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 AutoLayout 来配置我的表格视图单元格中的子视图,并且在理想情况下希望表格视图单元格与包含所有子视图所需的一样高.但是,这似乎是不可能的,因为单元格的高度是在实际创建单元格之前确定的.

I am trying to use AutoLayout to configure the subviews in my table view cells and in the ideal case would like the table view cell to be just as high as necessary to contain all the subviews. However, that does not seems to be possible, since the height for a cell is determined before the cell is actually created.

所以,现在,我只查看了我设置的约束并计算了包含所有内容所需的总高度,并将其在情节提要中设置为这个特定 TableViewCell 原型单元格的行高,我还在tableView:heightForRowAtIndexPath: 方法.

So, for now, I just looked at the constraints I set up and calculated the total height needed to contain everything and set that in the storyboard as the row height for this specific TableViewCell prototype cell and I also return this height in the tableView:heightForRowAtIndexPath: method.

现在,我的表格视图单元格看起来像这样(来自故事板的屏幕截图):

Now, my table view cell looks like this (screenshot from storyboard):

有两个没有显示大小的约束,它们的大小都是 10(按钮顶部容器视图以及滑块和中间标签之间的距离).

There are two constraints which don't have a size shown, they both have the size 10 (the button-top-container-view and the distance between the slider and the label in the middle).

从上到下会发生以下距离:

Going from top-to-bottom the following distance occur:

  • 10(距离)
  • 50(按钮高度)
  • 20(距离)
  • 20(标签高度)
  • 10(距离)
  • 30(滑块高度)
  • 20(距离)

导致总高度为 160.

leading to a total height of 160.

这正是我在这个单元的检查器中设置的:

That's exactly what I have set in the inspector of this cell:

但正如您从第一个屏幕截图中看到的那样,Interface builder 抱怨约束是冲突的(这就是它们显示为红色的原因).如果我将高度设置为 161,则 IB 很满意,但这是错误的.

but as you can see from the first screenshot, Interface builder complains that the constraints are conflicting (that's why they are displayed in red). IB is satisfied if I set the height to 161, but that is wrong.

另外,如果我这样做了,由于约束冲突,我会在运行时遇到异常:

Also, if I do, I get exceptions at runtime due to conflicting constraints:

Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
"<NSLayoutConstraint:0x8d80240 V:[UISlider:0x8d81190(30)]>",
"<NSLayoutConstraint:0x8d833e0 V:[UILabel:0x8d83300(20)]>",
"<NSLayoutConstraint:0x8d83750 V:[UIButton:0x8d83600(50)]>",
"<NSLayoutConstraint:0x8d85050 V:|-(10)-[UIButton:0x8d83600]   (Names: '|':UITableViewCellContentView:0x8d80ef0 )>",
"<NSLayoutConstraint:0x8d851d0 V:[UILabel:0x8d83300]-(10)-[UISlider:0x8d81190]>",
"<NSLayoutConstraint:0x8d85200 V:[UISlider:0x8d81190]-(20)-|   (Names: '|':UITableViewCellContentView:0x8d80ef0 )>",
"<NSLayoutConstraint:0x8d85320 V:[UIButton:0x8d83600]-(20)-[UILabel:0x8d83300]>",
"<NSAutoresizingMaskLayoutConstraint:0x8d8b7a0 h=--& v=--& V:[UITableViewCellContentView:0x8d80ef0(161)]>"

)

最后一个是我唯一没有明确设置的,但它似乎是从情节提要中的行高设置生成的,因为我的 tableView:heightForRowAtIndexPath: 方法返回 160:

the last one is the only one I did not explicitly set, but it seems to be generated from the row-height setting in the storyboard, since my tableView:heightForRowAtIndexPath: method returns 160:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == self.fetchedItemSetsController.fetchedObjects.count - 1)
    {
        return 160;
    }
    return 44;
}

(目前我总是希望最后一个单元格是大而特殊的)

(at the moment I always want the last cell to be the large and special one)

所以,好吧,当我将行高设置为 160 时,IB 会抱怨,但当我将其设置为 161 时,运行时会抱怨.所以我尝试忽略 IB 并将情节提要中的行高设置为 160(如第二个屏幕截图所示).在这种情况下,我会收到相同的错误消息,但差别很小:

So, okay, IB complains when I set the row height to 160, but when I set it to 161 the runtime complains. So I tried ignoring IB and set the row height to 160 in the storyboard as well (as seen in the second screenshot). In that case, I get the same error message with a tiny difference:

"<NSLayoutConstraint:0x17809ce80 V:[UISlider:0x125613660(30)]>",
"<NSLayoutConstraint:0x1782814f0 V:[UILabel:0x125615100(20)]>",
"<NSLayoutConstraint:0x178281a90 V:[UIButton:0x125615b50(50)]>",
"<NSLayoutConstraint:0x178281b80 V:|-(10)-[UIButton:0x125615240]   (Names: '|':UITableViewCellContentView:0x178161980 )>",
"<NSLayoutConstraint:0x178281c20 UIButton:0x1256157b0.height == UIButton:0x125615240.height>",
"<NSLayoutConstraint:0x178281d10 UIButton:0x1256157b0.height == UIButton:0x125615980.height>",
"<NSLayoutConstraint:0x178281e00 V:[UILabel:0x125615100]-(10)-[UISlider:0x125613660]>",
"<NSLayoutConstraint:0x178281e50 V:[UISlider:0x125613660]-(20)-|   (Names: '|':UITableViewCellContentView:0x178161980 )>",
"<NSLayoutConstraint:0x178281f40 UIButton:0x125615980.height == UIButton:0x125615b50.height>",
"<NSLayoutConstraint:0x178282030 V:[UIButton:0x125615240]-(20)-[UILabel:0x125615100]>",
"<NSAutoresizingMaskLayoutConstraint:0x178283340 h=--& v=--& V:[UITableViewCellContentView:0x178161980(159.5)]>"

表格视图单元格的高度约束现在是 159.5 而不是 160.我之前也遇到过 160.5,具有相同的情节提要设置,但我不确定它来自哪里.

The constraint for the height of the table view cell is now 159.5 instead of 160. I also encountered it as 160.5 before, with the same storyboard setting, but I am not sure where that came from.

所以,起初我认为这只是 IB 中的一个显示错误,但现在它实际上似乎是在创建错误的约束.它使用 160.5(或 159.5)而不是我指定的 160 创建一个约束.这是为什么呢?我该怎么办?

So, first I thought it was just a display bug in IB, but now it actually seems to be creating the wrong constraint. It creates a constraint with 160.5 (or 159.5) instead of the 160 I specified. Why is that? What can I do about it?

顺便说一句:单元格似乎显示正确,但我再次怀疑我能否看到 0.5 点的差异.主要是,我想摆脱异常,因为它们使调试更加困难,但我也想知道这里发生了什么.

Btw: The cells seems to be displayed correctly, but then again I doubt I would be able to see a 0.5 point difference. Mainly, I would like to get rid of the exceptions, since they make the debugging much harder, but I would also like to know what is going on here.

更新:159.5 不仅仅是因为我刚刚注意到的情节提要设置,它是情节提要设置和 tableView:heightForRowAtIndexPath: 的返回值的奇怪组合.以下是根据情节提要高度 (sb) 设置和 tableView:heightForRowAtIndexPath: 方法(方法)的返回值生成的 autoresizingmasklayoutconstraint 高度的几个示例.在所有情况下,子视图的约束都应导致高度为 160,并且不会更改.

UPDATE: The 159.5 is not JUST due to the storyboard setting I just noticed, it is a strange combination of the storyboard setting and the return value of the tableView:heightForRowAtIndexPath:. Here are a few examples of the height of the generated autoresizingmasklayoutconstraint depending on the storyboard height (sb) setting and the return value of the tableView:heightForRowAtIndexPath: method (method). In all cases, the constraints of the subview should lead to a height of 160 and are not changed.

  1. 160 (sb) &160(方法):159.5(约束)
  2. 161 (sb) &160(方法):159.5(约束)
  3. 160 (sb) &161(方法):160.5(约束)
  4. 162 (sb) &161(方法):162(约束)
  5. 161 (sb) &162(方法):161(约束)
  6. 162 (sb) &162(方法):162(约束)

所以我想,好吧,因为 (6.) 他们都同意,我将标签的高度设为 22,因此根据子视图的总高度也将是 162.结果:

So I thought, okay, for (6.) they all agree, I will just make the label's height 22, so the total height according to the subviews will be 162 as well. Result:

6a.162 (sb) &162(方法),162(子视图总高度):161.5(约束)

6a. 162 (sb) & 162 (method), 162 (total height of subviews): 161.5 (constraint)

什么?!

有什么想法吗?

更新 2我提供了一个示例项目,它重现了 在 Github 上的问题.

Update 2 I provided an example project that reproduces the issue on Github.

推荐答案

Separator 弄乱了 contentView 的高度,要么它应该被禁用(并替换为如果需要,请自定义一个),以便 contentView 高度与 cell 的高度匹配,或者考虑到 contentView 应将约束更改为更灵活高度可能与 cell 的高度不匹配.两者都做会更好.

Separator is messing with the height of the contentView, either it should be disabled (and replaced with custom one if required) so that the contentView height matches the height of the cell or the constraints should be changed to more flexible considering that the contentView height may mismatch the height of the cell. Doing both would be even better.

这篇关于NSAutoresizingMaskLayoutConstraint 的 UITableViewCell 舍入错误,但在 Storyboard 和 heightForRowAtIndexPath 中正确设置了大小:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:NSAutoresizingMaskLayoutConstraint 的 UITableViewCell 舍入错误,但在 Storyboard 和 heightForRowAtIndexPath 中正确设置了大小:

基础教程推荐