iOS - 表格视图单元格中的自动布局问题

Posted

技术标签:

【中文标题】iOS - 表格视图单元格中的自动布局问题【英文标题】:iOS - Autolayout issue in table view cell 【发布时间】:2018-04-14 17:05:35 【问题描述】:

我有一个包含以下项目的表格视图单元格:

图像视图 (343 x 193) 标签 1(高度:17) 标签 2(高度:20)

无论设备是什么,我都希望图像保持相同的纵横比(此应用仅适用于 iPhone,不适用于 iPad)。我正在做的是添加以下内容:

图像视图:纵横比约束;上、左、右约束

标签 1:高度限制;顶部(相对于图像视图),左侧, 正确的约束;等宽(带图像视图)

标签 2:高度限制;顶部(相对于图像视图),左侧, 右,底部(相对于单元格)约束;等宽(带 图像视图)

当我测试这个时,我得到以下结果

2018-04-14 11:50:33.457531-0500 MyAppios[4357:479920] [LayoutConstraints] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x60000009f9a0 UIImageView:0x7ff04952dfb0.width == 1.7772*UIImageView:0x7ff04952dfb0.height   (active)>",
    "<NSLayoutConstraint:0x60000009fc70 UILabel:0x7ff04952e1e0.height == 17   (active)>",
    "<NSLayoutConstraint:0x60000009fd60 UILabel:0x7ff04952e940'Loading...'.height == 20   (active)>",
    "<NSLayoutConstraint:0x60000009fea0 V:|-(10)-[UIImageView:0x7ff04952dfb0]   (active, names: '|':UITableViewCellContentView:0x7ff04952d770 )>",
    "<NSLayoutConstraint:0x60000009fef0 H:[UIImageView:0x7ff04952dfb0]-(16)-|   (active, names: '|':UITableViewCellContentView:0x7ff04952d770 )>",
    "<NSLayoutConstraint:0x60000009ff40 H:|-(16)-[UIImageView:0x7ff04952dfb0]   (active, names: '|':UITableViewCellContentView:0x7ff04952d770 )>",
    "<NSLayoutConstraint:0x60000009ff90 V:[UIImageView:0x7ff04952dfb0]-(8)-[UILabel:0x7ff04952e1e0]   (active)>",
    "<NSLayoutConstraint:0x600000280140 V:[UILabel:0x7ff04952e1e0]-(8)-[UILabel:0x7ff04952e940'Loading...']   (active)>",
    "<NSLayoutConstraint:0x6000002801e0 V:[UILabel:0x7ff04952e940'Loading...']-(10)-|   (active, names: '|':UITableViewCellContentView:0x7ff04952d770 )>",
    "<NSLayoutConstraint:0x60800009c070 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7ff04952d770.height == 266   (active)>",
    "<NSLayoutConstraint:0x60800009e230 'UIView-Encapsulated-Layout-Width' UITableViewCellContentView:0x7ff04952d770.width == 375   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x60000009fd60 UILabel:0x7ff04952e940'Loading...'.height == 20   (active)>

所以基本上是说它会修改第二个标签的高度以适应。

有没有更好的方法来设置这些约束?我应该比我正在使用的更多/更少吗?

【问题讨论】:

“有没有更好的方法来设置这些约束” 显然,是的。得到一个冲突错误是一个错误。你所做的是错误 “我应该比我正在使用的多/少吗?”冲突意味着您有太多约束。 你是使用静态高度还是使用UITableViewAutomaticDimension 使用静态高度,一切都通过故事板完成。 请提供一张粗略的图片,说明您希望如何设计您的 tableViewCell。 【参考方案1】:

该冲突背后的原因是单元格最初假定静态高度约束与子视图中的当前约束设置冲突。从UIView-Encapsulated-Layout-Height 可以清楚地看出这一点

所以设置label2的底部约束的优先级为999。

(另外,如果你设置了左右约束,不要设置宽度约束。)

【讨论】:

谢谢你,Sh_Khan,将底部约束设置为 999 消除了错误。

以上是关于iOS - 表格视图单元格中的自动布局问题的主要内容,如果未能解决你的问题,请参考以下文章

为啥表格视图单元格中的自动布局会导致表格的内容大小被错误地更改?

使用自动布局表格视图时如何增加表格视图单元格中的标签高度(取决于文本)?

如何使用自动布局来调整表格视图单元格中的视图大小?

表格视图单元格中的 ImageView 切断标签(自动布局)[关闭]

iOS 自动布局:表格视图单元格宽度 = 0

如何使用自动布局来调整表格视图单元格中的视图?