NSLayoutConstraint 中断

NSLayoutConstraint breaks

我有一个自定义的 UITableViewCell。该单元格具有三个标签。最左边的 'Amount' 标签具有以下约束。

在单元格的右侧,我有另一个标签,'Label Dollar Amount'。它具有以下约束:

'Label Dollar Amount' 正下方的第三个标签是 'Label Max Amount'。约束条件如下:

代码没有做任何事情。 table 行高是使用 UITableViewAutomaticDimension 计算的。 estimatedRowHeight 设置为 100。

当视图处于横向并且 table 视图向下滚动时,这个特定的单元格正在出列,因为它不在可见视图中。当方向更改为相同状态的纵向时,单元格应该是可见的。

我收到以下错误:

 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. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
NSLayoutConstraint:0x1607a770 H:[UILabel:0x160c1990'Amount'(150)],
NSLayoutConstraint:0x160c2b30 H:|-(18)-[UILabel:0x160c1990'Amount']   (Names: '|':UITableViewCellContentView:0x160c18c0 ),
NSLayoutConstraint:0x160c2b90 H:[UILabel:0x160c1990'Amount']-(0)-[UILabel:0x160c1c40'.61'],
NSLayoutConstraint:0x160c2bf0 UILabel:0x160c1ac0'(Max: ,000.00)'.leading == UILabel:0x160c1c40'.61'.leading,
NSLayoutConstraint:0x160c2c80 H:[UILabel:0x160c1ac0'(Max: ,000.00)']-(12)-|   (Names: '|':UITableViewCellContentView:0x160c18c0 ),
NSLayoutConstraint:0x14e179e0 'UIView-Encapsulated-Layout-Width' H:[UITableViewCellContentView:0x160c18c0(72)]
)

Will attempt to recover by breaking constraint 
NSLayoutConstraint:0x1607a770 H:[UILabel:0x160c1990'Amount'(150)]`

注意:对于此处看到的所有可能的约束,优先级都设置为 1000(必需)。

如果我将 Amount 标签上的宽度限制的优先级更改为 999,它就会起作用。但是对于较长的文本,标签会消失。由于尾随约束中的零间距具有较高的优先级。有什么可能出错的建议吗?

table 将您的单元格视图设置为 72 点的固定宽度。这可能只是暂时的,因为它会适应新的方向或布局,但您需要适应它:

NSLayoutConstraint:0x14e179e0 'UIView-Encapsulated-Layout-Width' H:[UITableViewCellContentView:0x160c18c0(72)]

因此,您需要决定当其父视图只有 72 点宽时您的布局中应该发生什么。

目前,您有以下内容(稍作编辑以使事情更清楚):

H:|-(18)-[UILabel:0x160c1990'Amount']   (Names: '|':UITableViewCellContentView:0x160c18c0 ),
H:[UILabel:0x160c1990'Amount'(150)],
H:[UILabel:0x160c1990'Amount']-(0)-[UILabel:0x160c1c40'.61'],
UILabel:0x160c1ac0'(Max: ,000.00)'.leading == UILabel:0x160c1c40'.61'.leading,
H:[UILabel:0x160c1ac0'(Max: ,000.00)']-(12)-|   (Names: '|':UITableViewCellContentView:0x160c18c0 ),

或者:

H:|-(18)-[Amount(150)][Max]-(12)-|

因此,即使每个视图都可以折叠为 0 宽度,您仍然需要在超级视图的边缘之间有 18 + 150 + 12 == 180 点的宽度。这与可用的 72 点冲突。

您希望如何解决?正如您提到的,一种可能性是降低 Amount 标签上宽度约束的优先级。或者您甚至可以完全消除此约束并依赖固有宽度和压缩阻力优先级。你说 "the label disappears for longer text. Since zero spacing in the trailing constraint has a hight priority." 我不完全确定你的意思。也许对于较长的文本,标签正在换行并且单词位于不可见的第二行?如果是这样,您可以将其设置为截断(尽管这应该是默认设置)。如果连截断都不接受table,那么会发生什么?

您可以尝试设置 adjustsFontSizeToFitWidthminimumScaleFactor 以允许标签中的文本缩小,但最终您仍然达到限制(如果文本足够长且单元格宽度较窄够了),所以你仍然需要决定你想要发生什么。

另一种方法是降低 Label Max Amount 和 superview 之间的尾部约束的优先级,以便该标签从右侧移开。您可能还需要降低 Label Dollar Amount 的尾随约束的优先级,因为它对布局施加了相同的要求。

但是,当然,这意味着您的某些内容不在屏幕上。无法回避的事实是,当宽度太小无法容纳所有内容时,您必须决定应该丢失什么。

一旦你决定了应该发生什么,你就设定了实现它的优先级。例如,如果您希望先截断 Max Amount,请将其压缩阻力设置为最低。如果您希望下一个截断美元金额,请将其压缩设置为第二低。如果您希望 Amount 标签在此之后被截断,请将其宽度约束(或压缩阻力,如果您摆脱宽度约束)设置为第三低。如果你想把事情推到右边缘作为最后的手段,将 trailing-to-superview 约束的优先级设置为 999。