UITableViewCell中UILabels的模糊布局警告
我在UITableViewCell
中有两个彼此相邻的UILabel
视图。左边有一条线,右边可以有多条线并使用剩下的任何水平空间。两个标签距离单元格顶部的距离相同。单元格的高度由正确标签的高度决定。在某些情况下,我发现上面和下面有不需要的额外空间,因此它们不是顶端对齐的。仔细研究它,我发现hasAmbiguousLayout
为这两个标签返回YES
。UITableViewCell中UILabels的模糊布局警告
当我在调试器中调用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:下面是我对内容查看的约束和两个标签:
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>"
)
这里是在我有约束的画面,什么不顺心:
我解决它通过添加合适的UILabel
:
[self.bodyLabel setContentHuggingPriority: UILayoutPriorityFittingSizeLevel forAxis: UILayoutConstraintAxisHorizontal];
的另一件事是,我是为歧义updateConstraints
测试,而我应该在layoutSubviews
谢谢!它帮助了我。 – 2017-10-06 17:44:03
啊!您是否关闭了cell.contentView的视图层次结构中的每个子视图的 translatesAutoresizingMaskIntoConstraints
属性?
输出显示您的自动布局与默认自动调整行为之间的冲突证据。在xib中,可以通过复选框关闭所有内容,但在代码中,您必须在每个视图级别上执行此操作。
PureLayout框架已经考虑到了这一点。 – Koen 2015-02-24 14:40:18
不是根据您给出的输出,而是显式列出自动调整限制条目。 HAS:| - (0) - [UITableViewCellContentView:0x7b768fc0](名称:'|':MyTVCell:0x7b769330' MyTVCell')>''' – Cocoadelica 2015-02-24 14:54:50
有趣。在我检查'constraintsAffectingLayoutForAxis','self.bodyLabel'的同一点。translatesAutoresizingMaskIntoConstraints' returns'NO' – Koen 2015-02-24 15:37:14
末已经做了你在xib/storyboard或代码中进行布局? – Cocoadelica 2015-02-24 14:07:46
在代码中,使用PureLayout – Koen 2015-02-24 14:15:09