【发布时间】: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