【问题标题】:Ambiguous layout warnings for UILabels in UITableViewCellUITableViewCell 中 UILabel 的不明确布局警告
【发布时间】:2015-04-26 02:29:14
【问题描述】:

UITableViewCell 中有两个相邻的 UILabel 视图。左边的有一条线,右边的可以有多条线,并使用剩下的任何水平空间。两个标签与单元格顶部的距离相同。单元格的高度由右标签的高度决定。在某些情况下,我会在 UILabel 的右侧上方和下方看到不需要的额外空间,因此它们不是顶部对齐的。进一步研究,我发现hasAmbiguousLayout 为两个标签返回YES

当我在调试器中调用constraintsAffectingLayoutForAxis 时,我得到以下输出:

(lldb) po [0x7b769520 constraintsAffectingLayoutForAxis:0]
<__NSArrayM 0x7b6cb340>(
<NSAutoresizingMaskLayoutConstraint:0x7b76abf0 h=--& v=--& 'UIView-Encapsulated-Layout-Left' H:|-(0)-[UITableViewCellContentView:0x7b768fc0]   (Names: '|':MyTVCell:0x7b769330'MyTVCell' )>,
<NSLayoutConstraint:0x7b76b010 H:|-(15)-[UILabel:0x7b769520'Number']   (Names: '|':UITableViewCellContentView:0x7b768fc0 )>,
<NSContentSizeLayoutConstraint:0x7b769a90 H:[UILabel:0x7b769520'Number'(131)] Hug:250 CompressionResistance:750>
)

对于位于第一个 UILabel 右侧的第二个 UILabel,我得到了这个:

(lldb) po [0x7b769710 constraintsAffectingLayoutForAxis:0]
<__NSArrayM 0x7b6cb9e0>(
<NSContentSizeLayoutConstraint:0x7b769a90 H:[UILabel:0x7b769520'Number'(131)] Hug:250 CompressionResistance:750>,
<NSAutoresizingMaskLayoutConstraint:0x7b76abf0 h=--& v=--& 'UIView-Encapsulated-Layout-Left' H:|-(0)-[UITableViewCellContentView:0x7b768fc0]   (Names: '|':MyTVCell:0x7b769330'MyTVCell' )>,
<NSLayoutConstraint:0x7b76b010 H:|-(15)-[UILabel:0x7b769520'Number']   (Names: '|':UITableViewCellContentView:0x7b768fc0 )>,
<NSLayoutConstraint:0x7b76b230 H:[UILabel:0x7b769520'Number']-(15)-[UILabel:0x7b769710'Q12472']>,
<NSLayoutConstraint:0x7b76ab90 'UIView-Encapsulated-Layout-Width' H:[UITableViewCellContentView:0x7b768fc0(320)]>,
<NSLayoutConstraint:0x7b76b310 UILabel:0x7b769710'Q12472'.trailing == UITableViewCellContentView:0x7b768fc0.trailing - 15>
)

谁能根据上面的输出解释为什么这些标签的布局不明确?

更新:NSAutoresizingMaskLayoutConstraint 约束属于单元格的contentView。两个标签都将translatesAutoresizingMaskIntoConstraints 设置为NO

更新 2: 以下是我对 contentView 和两个标签的限制:

2015-02-26 07:35:25.559 contentView constraints: (
    "<NSLayoutConstraint:0x7be541e0 V:|-(8)-[UILabel:0x7be537d0]   (Names: '|':UITableViewCellContentView:0x7be535a0 )>",
    "<NSLayoutConstraint:0x7be54240 H:|-(15)-[UILabel:0x7be537d0]   (Names: '|':UITableViewCellContentView:0x7be535a0 )>",
    "<NSLayoutConstraint:0x7be54310 V:|-(8)-[UILabel:0x7be53990]   (Names: '|':UITableViewCellContentView:0x7be535a0 )>",
    "<NSLayoutConstraint:0x7be54340 H:[UILabel:0x7be537d0]-(15)-[UILabel:0x7be53990]>",
    "<NSLayoutConstraint:0x7be54370 UILabel:0x7be53990.trailing == UITableViewCellContentView:0x7be535a0.trailing - 15>",
    "<NSLayoutConstraint:0x7be543c0 UILabel:0x7be53990.bottom == UITableViewCellContentView:0x7be535a0.bottom - 8>",
    "<NSContentSizeLayoutConstraint:0x7be53cd0 H:[UILabel:0x7be537d0(109)] Hug:250 CompressionResistance:750>",
    "<NSContentSizeLayoutConstraint:0x7be53d10 V:[UILabel:0x7be537d0(21)] Hug:250 CompressionResistance:750>",
    "<NSContentSizeLayoutConstraint:0x7be53eb0 H:[UILabel:0x7be53990(20)] Hug:250 CompressionResistance:750>",
    "<NSContentSizeLayoutConstraint:0x7be53f10 V:[UILabel:0x7be53990(20)] Hug:250 CompressionResistance:750>"
)
2015-02-26 07:35:25.560 left label constraints: (
    "<NSContentSizeLayoutConstraint:0x7be53cd0 H:[UILabel:0x7be537d0(109)] Hug:250 CompressionResistance:750>",
    "<NSContentSizeLayoutConstraint:0x7be53d10 V:[UILabel:0x7be537d0(21)] Hug:250 CompressionResistance:750>"
)
2015-02-26 07:35:25.561 right label constraints:(
    "<NSContentSizeLayoutConstraint:0x7be53eb0 H:[UILabel:0x7be53990(20)] Hug:250 CompressionResistance:750>",
    "<NSContentSizeLayoutConstraint:0x7be53f10 V:[UILabel:0x7be53990(20)] Hug:250 CompressionResistance:750>"
)

这是一张图片,显示了我的限制以及出了什么问题:

【问题讨论】:

  • 您是在 xib/storyboard 中还是在代码中进行布局?
  • 在代码中,使用 PureLayout

标签: ios uitableview autolayout uilabel


【解决方案1】:

啊!你有没有关掉 translatesAutoresizingMaskIntoConstraints cell.contentView 的视图层次结构中的每个子视图的属性?

输出显示您的自动布局与默认自动调整大小行为之间存在冲突的证据。在 xibs 中,可以通过复选框关闭所有内容,但在代码中,您必须在每个视图级别执行此操作。

【讨论】:

  • 不是根据您给出的输出,它明确列出了自动调整大小的约束条目。 &lt;NSAutoresizingMaskLayoutConstraint:0x7b76abf0 h=--&amp; v=--&amp; 'UIView-Encapsulated-Layout-Left' H:|-(0)-[UITableViewCellContentView:0x7b768fc0] (Names: '|':MyTVCell:0x7b769330'MyTVCell' )&gt;
  • 有趣。在我检查constraintsAffectingLayoutForAxis 的同时,self.bodyLabel.translatesAutoresizingMaskIntoConstraints 返回NO
  • 阅读更新,这很酷。在最初的问题中,您说单元格的高度(因此是 contentView)由正确标签的高度定义?这通常通过向 contentView 添加一个约束来实现,该约束等于正确标签的高度或至少一个最小高度。由于您正在应用(直接通过自动布局或通过 PureLayout 间接)对 contentView 的约束,并且自动调整大小消息与 contentView 相关,因此可能不存在冲突吗?
  • 我在另一个更新中添加了 contentView 的所有约束和两个标签。我认为它涵盖了右标签和 contentView 之间的顶部和底部约束。
  • 我在 UICollectionViewCell 中使用 UIWebView。我已将每个子视图的 translatesAutoresizingMaskIntoConstraints 设置为 false,但仍然出现相同的错误。 UIWebView 的位置和高度不明确。
【解决方案2】:

我通过添加正确的UILabel来解决它:

[self.bodyLabel setContentHuggingPriority: UILayoutPriorityFittingSizeLevel forAxis: UILayoutConstraintAxisHorizontal];

另一件事是我正在测试updateConstraints 中的歧义,而我应该在layoutSubviews 末尾完成它

【讨论】:

    猜你喜欢
    • 2013-10-27
    • 1970-01-01
    • 2012-10-15
    • 2013-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多