objective-c iOS8 - 约束模棱两可地建议高度为零
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25822324/
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
iOS8 - constraints ambiguously suggest a height of zero
提问by Chris
Has anyone got any idea how to debug this?
有没有人知道如何调试这个?
Warning once only: Detected a case where constraints ambiguously suggest a height of zero for a tableview cell's content view. We're considering the collapse unintentional and using standard height instead.
仅警告一次:检测到约束模糊地建议 tableview 单元格的内容视图的高度为零的情况。我们正在考虑无意识的坍塌并使用标准高度。
The rows have a fixed height as set by
行具有由设置的固定高度
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 34.0;
}
And all the constraintsseem to be happy...
而所有的人constraints似乎都很高兴......
回答by FBronner
Forcing a return height and estimated height made the warning disappear in my case.
强制返回高度和估计高度使警告在我的情况下消失。
- (CGFloat)tableView:(UITableView *)tableView
estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 44;
}
- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 44;
}
Another solution where you don't need the two overrides is simply to use self.tableView.rowHeight = 44;in your loadViewor init method.
不需要这两个覆盖的另一种解决方案是简单地self.tableView.rowHeight = 44;在您的loadView或 init 方法中使用。
回答by MonsieurDart
What can also be done is adding vertical constraints from the top and to the bottom of the content view. This will make autolayout happy (because he now knows how to calculate the height of the cell himself).
还可以做的是从内容视图的顶部到底部添加垂直约束。这将使自动布局愉快(因为他现在知道如何自己计算单元格的高度)。
回答by user2898617
If you're using autoLayout constraints and UITableViewAutomaticDimension, this error is not some erroneous problem to be discarded by overriding your height in code. It means that determining the cell height automatically isn't working because you don't have the proper vertical constraints needed.
如果您使用 autoLayout 约束和 UITableViewAutomaticDimension,则此错误不是通过在代码中覆盖您的高度来丢弃的错误问题。这意味着自动确定单元格高度不起作用,因为您没有所需的适当垂直约束。
If you're like me and were getting this error and needed help identifying which cell was throwing the error, you can add the following line right before the return of your 'heightforRowAtIndexPath' method.
如果您像我一样收到此错误并需要帮助确定哪个单元格引发错误,您可以在返回“heightforRowAtIndexPath”方法之前添加以下行。
NSLog(@"Section %ld Row %ld", (long)[indexPath section], (long)[indexPath row]);
This will print out a long list of sections and rows, but the error will appear immediately following the particular cell that is causing the error, and you can quickly identify which cell is causing the problem and fix your constraints accordingly. This is particularly helpful for static cells. Overriding the height with a manually entered number will work if you're not using autoLayout and automatic cell heights, but will essentially disable these features which is a very poor solution if its something you're trying to utilize.
这将打印出一长串部分和行,但错误将立即出现在导致错误的特定单元格之后,您可以快速确定导致问题的单元格并相应地修复您的约束。这对静态单元格特别有用。如果您不使用 autoLayout 和自动单元格高度,则使用手动输入的数字覆盖高度将起作用,但实际上会禁用这些功能,如果您尝试使用这些功能,这是一个非常糟糕的解决方案。
If you weren't previously using the 'heightForRowAtIndexPath' method but want to debug this error without undoing your UITableViewAutomaticDimension setting, simply add this to your code:
如果您之前没有使用 'heightForRowAtIndexPath' 方法,但想在不撤消 UITableViewAutomaticDimension 设置的情况下调试此错误,只需将其添加到您的代码中:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"Section %ld Row %ld", (long)[indexPath section], (long)[indexPath row]);
return UITableViewAutomaticDimension;
}
回答by ltm
There appears to be a bug in XCode 6.1 that causes this problem if using auto-layout and you don't specify a value for the Row Height for each Table View Cell, but instead you leave the "default" value. Simply checking the "Custom" checkbox next to the Row Height, for every cell, makes the warning go away.
XCode 6.1 中似乎存在一个错误,如果使用自动布局并且您没有为每个表视图单元格指定行高值,而是保留“默认”值,则会导致此问题。只需选中每个单元格行高旁边的“自定义”复选框,警告就会消失。
回答by Juraj Antas
Yes, You get all constrains "happy" even in case when you only have horizontal constrains for items in table view cell. I had same issue. You need to add also vertical constrains. Doing so, that warning will go away.
是的,即使您对表格视图单元格中的项目只有水平约束,您也会“满意”所有约束。我有同样的问题。您还需要添加垂直约束。这样做,该警告将消失。
回答by Woodster
The constraints can be happy for the purpose of layout, but not happy for the purpose of automatic row height. A happy layout would mean the content can be laid out without ambiguity. That would satisfy the checks in Interface Builder.
约束可以满足布局的目的,但不能满足自动行高的目的。一个愉快的布局意味着内容可以毫无歧义地布局。这将满足 Interface Builder 中的检查。
A happy layout for automatic row height would mean that, in addition to the above, you're also including constraints to the bottom of the cell.
自动行高的愉快布局意味着,除了上述内容之外,您还包括对单元格底部的约束。
More here: Detected a case where constraints ambiguously suggest a height of zero
回答by teho
I used Row Height 43 (or <> 44) in the Table View size inspector and the error disappeared. Using 44 I get the error. Xcode version 6.0.1.
我在表格视图大小检查器中使用了 Row Height 43(或 <> 44),错误消失了。使用 44 我得到错误。Xcode 版本 6.0.1。
-- This answer was removed by a moderator, please don't, it fixes the problem. This SOLVES the problem for me and may do it for others too. So could you be so kind not to delete it again.
-- 这个答案被版主删除了,请不要,它解决了问题。这为我解决了问题,也可以为其他人解决问题。所以你能不能不要再删除它了。
回答by wrightak
If you're getting that warning, it's most likely because you're using auto layout and your cells don't have any constraints inside them.
如果您收到该警告,很可能是因为您使用的是自动布局,并且您的单元格内部没有任何约束。
You should either stop using auto layout or implement constraints that unambiguously define the height of the cells.
您应该停止使用自动布局或实施明确定义单元格高度的约束。
You can turn auto layout off in interface builder by unchecking the "Use Autolayout" option in the file inspector on the right.
您可以通过取消选中右侧文件检查器中的“使用自动布局”选项来关闭界面构建器中的自动布局。
If you choose to use auto layout and the height of your cells is fixed, implementing the appropriate constraints should be easy. Simply add height constraints for subviews of the cell's content view, and implement vertical space constraints between the subviews, and between the subviews and the content view. For example if your cell has one label in it, this would work:
如果您选择使用自动布局并且单元格的高度是固定的,那么实施适当的约束应该很容易。只需为cell的内容视图的子视图添加高度约束,并在子视图之间,以及子视图和内容视图之间实现垂直空间约束。例如,如果您的单元格中有一个标签,这将起作用:
Vertical constraints
垂直约束
- Vertical space constraint between the top of the content view and the top of the label
- Fixed height constraint of label
- Vertical space constraint between the bottom of the label and the bottom of the content view
- 内容视图顶部和标签顶部之间的垂直空间约束
- 标签的固定高度约束
- 标签底部和内容视图底部之间的垂直空间约束
Horizontal constraints
水平约束
- Horizontal space constraint between the leading edge of the content view and the leading edge of the label
- Fixed width constraint of label
- Horizontal space constraint between the trailing edge of the label and the trailing edge of the content view
- 内容视图前缘和标签前缘之间的水平空间约束
- 标签的固定宽度约束
- 标签后缘和内容视图后缘之间的水平空间约束
回答by amir
I couldn't get to remove the warning, but to make constraints work I set the ,new to iOS8 , tableview property estimatedRowHeightto the fixed height, and removed heightForRowAtIndexPathimplementation.
我无法删除警告,但为了使约束起作用,我将 ,new 设置为 iOS8 ,将 tableview 属性estimatedRowHeight设置为固定高度,并删除了heightForRowAtIndexPath实现。
回答by ricardopereira
You can use AutoLayout to calculate the right height for you. Here is a nice post about Dynamic Cell Height on iOS 8: http://natashatherobot.com/ios-8-self-sizing-table-view-cells-with-dynamic-type/
您可以使用 AutoLayout 为您计算正确的高度。这是一篇关于 iOS 8 上动态单元格高度的好文章:http: //natashatherobot.com/ios-8-self-sizing-table-view-cells-with-dynamic-type/

