ios 检测到约束模棱两可地暗示高度为零的情况
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25902288/
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
Detected a case where constraints ambiguously suggest a height of zero
提问by David E
After updating to Xcode 6.1 beta 2 when I run my app that contains tableview cells, the debug assistant says:
当我运行包含 tableview 单元格的应用程序更新到 Xcode 6.1 beta 2 后,调试助手说:
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.
Before, when I used Xcode 5 on this project, I would get a few errors but those have gone away since I upgraded. I have no other errors or warnings now. I have already tried adjusting the sizes of all the tableview cells and also tried using standard height but I still get the same warning:
之前,当我在这个项目中使用 Xcode 5 时,我会遇到一些错误,但自从我升级后这些错误都消失了。我现在没有其他错误或警告。我已经尝试调整所有 tableview 单元格的大小,也尝试使用标准高度,但我仍然收到相同的警告:
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.
I have also read through all similar topics on this but none of their solutions help. When I test the app with the simulator, the app runs fine except the pictures that are supposed to be in the tableView cells aren't there.
我也通读了所有类似的主题,但他们的解决方案都没有帮助。当我使用模拟器测试应用程序时,应用程序运行良好,但应该在 tableView 单元格中的图片不存在。
回答by Woodster
You're encountering the side effect of a fantastic new feature in iOS8's Tableviews: Automatic Row Heights.
您遇到了 iOS8 的 Tableviews 中一个奇妙的新功能的副作用:自动行高。
In iOS 7, you either had rows of a fixed size (set with tableView.rowHeight
), or you'd write code to calculate the height of your cells and you'd return that in tableView:heightForRowAtIndexPath
. Writing code for the calculation of a cell's height could be quite complex if you had numerous views in your cell and you had different heights to consider at different font sizes. Add in Dynamic Type and the process was a pain in the ass.
在 iOS 7 中,您要么有固定大小的行(用 设置tableView.rowHeight
),要么编写代码来计算单元格的高度,然后在tableView:heightForRowAtIndexPath
. 如果您的单元格中有多个视图并且您在不同的字体大小下需要考虑不同的高度,那么编写用于计算单元格高度的代码可能会非常复杂。添加动态类型,这个过程很痛苦。
In iOS 8, you can still do the above, but now the height of the rows can be determined by iOS, provided that you've configured the content of your cell using Auto Layout. This is huge benefit for developers, because as the dynamic font size changes, or the user modifies the text size using Accessibility Settings, your UI can be adaptive to the new size. It also means if you have a UILabel that can have multiple rows of text, your cell can now grow to accommodate those when the cells needs to, and shrink when it does not, so there isn't any unnecessary whitespace.
在 iOS 8 中,您仍然可以执行上述操作,但现在行的高度可以由 iOS 确定,前提是您已使用自动布局配置了单元格的内容。这对开发人员来说是巨大的好处,因为随着动态字体大小的变化,或者用户使用辅助功能设置修改文本大小,您的 UI 可以适应新的大小。这也意味着,如果您有一个可以包含多行文本的 UILabel,您的单元格现在可以在单元格需要时增长以容纳那些,并在不需要时缩小,因此没有任何不必要的空白。
The warning message you're seeing is telling you that there aren't enough constraints in your cell for Auto Layout to inform the tableview of the height of the cell.
您看到的警告消息告诉您,您的单元格中没有足够的约束来让自动布局通知表格视图单元格的高度。
To use dynamic cell height, which, along with the techniques already mentioned by other posters, will also get rid of this message, you need to ensure your cell has sufficient constraints to bind the UI items to the top and bottomof the cell. If you've used Auto Layout before, you are probably accustomed to setting Top + Leading constraints, but dynamic row height also requires bottom constraints.
要使用动态单元格高度,连同其他海报已经提到的技术,也将摆脱此消息,您需要确保您的单元格具有足够的约束以将 UI 项绑定到单元格的顶部和底部。如果你之前用过 Auto Layout,你可能已经习惯了设置 Top + Leadership 约束,但动态行高也需要底部约束。
The layout pass works like this, which occurs immediately before a cell is displayed on screen, in a just-in-time manner:
布局过程是这样工作的,它以即时方式在单元格显示在屏幕上之前立即发生:
Dimensions for content with intrinsic sizes is calculated. This includes UILabels and UIImageViews, where their dimensions are based on the text or UIImages they contain, respectively. Both of these views will consider their width to be a known (because you've set constraints for trailing/leading edges, or you set explicit widths, or you used horizontal constraints that eventually reveal a width from side to side). Let's say a label has a paragraph of text ("number of lines" is set to 0 so it'll auto-wrap), it can only be 310 points across, so it's determined to be 120pt high at the current font size.
The UI is laid out according to your positioning constraints. There is a constraint at the bottom of the label that connects to the bottom margin of the cell. Since the label has grown to be 120 points tall, and since it's bound to the bottom of the cell by the constraint, it must push the cell "down" (increasing the height of the cell) to satisfy the constraint that says "bottom of the label is always standard distance from the bottom of the cell.
计算具有固有尺寸的内容的尺寸。这包括 UILabels 和 UIImageViews,它们的尺寸分别基于它们包含的文本或 UIImages。这两个视图都将它们的宽度视为已知的(因为您已经为后缘/前缘设置了约束,或者您设置了明确的宽度,或者您使用了最终显示左右宽度的水平约束)。假设一个标签有一段文本(“行数”设置为 0,所以它会自动换行),它只能是 310 点,因此在当前字体大小下它被确定为 120pt 高。
UI 根据您的定位限制进行布局。标签底部有一个连接到单元格底部边距的约束。由于标签已经长到 120 点高,并且因为它被约束绑定到单元格的底部,所以它必须“向下”推动单元格(增加单元格的高度)以满足“底部”的约束标签始终与单元格底部的标准距离。
The error message you reported occurs if that bottom constraint is missing, in which case there is nothing to "push" the bottom of the cell away from the top of the cell, which is the ambiguity that's reported: with nothing to push the bottom from the top, the cell collapses. But Auto Layout detects that, too, and falls back to using the standard row height.
如果缺少底部约束,则会出现您报告的错误消息,在这种情况下,没有任何东西可以将单元格的底部“推”离单元格的顶部,这是报告的歧义:没有任何东西可以推动底部顶部,细胞崩溃。但自动布局也会检测到这一点,并回退到使用标准行高。
For what it's worth, and mostly to have a rounded answer, if you do implement iOS 8's Auto Layout-based dynamic row heights, you should implement tableView:estimatedHeightForRowAtIndexPath:
. That estimate method can use rough values for your cells, and it'll be called when the table view is initially loaded. It helps UIKit draw things like the scrollbar, which can't be drawn unless the tableview knows how much content it can scroll through, but does't need totally accurate sizes, since it's just a scrollbar. This lets the calculation of the actual row height be deferred until the moment the cell is needed, which is less computationally intensive and lets your UITableView be presented quicker.
对于它的价值,并且主要是为了获得全面的答案,如果您确实实现了 iOS 8 的基于自动布局的动态行高,则应该实现tableView:estimatedHeightForRowAtIndexPath:
. 该估计方法可以为您的单元格使用粗略的值,并且会在最初加载表视图时调用它。它帮助 UIKit 绘制滚动条之类的东西,除非 tableview 知道它可以滚动多少内容,否则无法绘制滚动条,但不需要完全准确的大小,因为它只是一个滚动条。这允许将实际行高的计算推迟到需要单元格的那一刻,这样计算量较小,并且可以更快地呈现 UITableView。
回答by Viktor Kucera
Three things have managed to silence this warning so far. You can pick up the most convenient for you. Nothing pretty though.
到目前为止,三件事已经成功地消除了这个警告。您可以选择最方便的。不过没什么好看的。
To set up default cell's height in viewDidLoad
self.tableView.rowHeight = 44;
Go to storyboard and change row height on your tableview to something different than 44.
To implement tableview's delegate method heightForRowAtIndexPath
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 44; }
在 viewDidLoad 中设置默认单元格的高度
self.tableView.rowHeight = 44;
转到故事板并将 tableview 上的行高更改为 44 以外的值。
实现tableview的委托方法heightForRowAtIndexPath
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 44; }
Weird.
奇怪的。
回答by ABakerSmith
I had this problem after creating a custom UITableViewCell
and adding my subviews to the cell instead of its contentView
.
在创建自定义UITableViewCell
并将我的子视图添加到单元格而不是它的contentView
.
回答by Anconia
To resolve this without a programmatic method, adjust the row height of the table view in the Size Inspector from the storyboard.
要在没有编程方法的情况下解决此问题,请从故事板调整大小检查器中表格视图的行高。
回答by Alok
This is an autolayout issue. Make sure that your subviews have all the constraints. For me, the bottom constraint was missing for the Title Label in the cell. When I added that, the warning went away and everything showed up perfectly.
这是一个自动布局问题。确保您的子视图具有所有约束。对我来说,单元格中的标题标签缺少底部约束。当我添加它时,警告消失了,一切都完美地显示出来。
回答by Hyman
Just enable Self-Sizing Table View Cells
只需启用自调整表格视图单元格
tableView.estimatedRowHeight = 85.0
tableView.rowHeight = UITableViewAutomaticDimension
& make sure you added constraints on all sides of UITableViewCell
as-
并确保您在UITableViewCell
as- 的所有方面都添加了约束-
回答by Amit Verma
回答by humblePilgrim
I got this warning today. Here is what made it disappear for me(in interface builder)
我今天收到了这个警告。这是让我消失的原因(在界面构建器中)
1.Set the row height field for the table view to something other than 44 2 Set the row height field for the tableView cell to something other than 44
1.将表格视图的行高字段设置为 44 以外的其他内容 2 将 tableView 单元格的行高字段设置为 44 以外的其他内容
I did not have to make any changes in code
我不必对代码进行任何更改
回答by gohnjanotis
In my case, I was building the cell programmatically and kept getting this error.
就我而言,我正在以编程方式构建单元并不断收到此错误。
I was adding the subviews and constraints in the UITableViewCell
's init
method like this:
我在UITableViewCell
'sinit
方法中添加了子视图和约束,如下所示:
addSubview(rankingLabel)
addConstraints(cellConstraints)
I solved the issue by adding them to the cell's contentView
instead:
我通过将它们添加到单元格contentView
来解决这个问题:
contentView.addSubview(rankingLabel)
contentView.addConstraints(cellConstraints)