我正在另一个视图中构造一个可变大小的表视图。表格视图不应滚动,因此我将以编程方式确定其内容大小并调整高度限制,以使表格视图始终适合其内容。

我遇到的问题是有关约束损坏的警告:

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.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0x170498b00 h=--& v=--& UIView:0x1024f2c90.height == 322   (active)>",
"<NSLayoutConstraint:0x17428d660 UITableView:0x103152400.height == 322   (active)>",
"<NSLayoutConstraint:0x174482da0 V:[UITableView:0x103152400]-(27)-|   (active, names: '|':UIView:0x1024f2c90 )>",
"<NSLayoutConstraint:0x17429da10 V:|-(16)-[UITableView:0x103152400]   (active, names: '|':UIView:0x1024f2c90 )>"
)

为了解决这个问题,我尝试了两件事:
  • 设置translationsAutoresizingMaskIntoConstraints = false。这会导致一切都变得与表格视图无关。
  • 将可调高度约束的优先级降低到999。

  • 第二点解决了我的问题,我可以使用较低优先级约束来调整视图的高度。但是,我不明白为什么会这样。

    那么,“自动布局”如何解释约束的优先级?我本来希望NSAutoresizingMaskLayoutConstraint能够接管较低优先级的约束并使之生效,所以我无法使用约束来调整视图的大小。

    最佳答案

    要回答标题中的问题:降低约束的优先级将告诉自动布局,该约束比具有更高优先级的所有约束不那么重要。

    这意味着,如果两个约束冲突,则自动布局将使用优先级最高的一个,而忽略另一个。

    如果两个优先级相同的必需约束冲突,您将收到一条错误消息,如您所描述的。

    10-07 19:43
    查看更多