降低约束的优先级到底有什么作用?
What exactly does lowering the priority of a constraint do?
我正在另一个视图中构建可变大小 table 视图。 table 视图不应滚动,因此我以编程方式确定其内容大小并调整高度限制,以便 table 视图始终适合其内容。
我 运行 遇到的问题是有关破坏约束的警告:
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 )>"
)
为了解决这个问题,我尝试了两件事:
- 设置 translatesAutoresizingMaskIntoConstraints = false。这会导致 table 视图出现问题。
- 将 adjustable 高度限制的优先级降低到 999。
第 2 点解决了我的问题,我可以使用较低优先级约束来调整视图的高度。但是,我不明白为什么会这样。
那么,自动布局如何解释约束的优先级?我本来希望 NSAutoresizingMaskLayoutConstraint 接管较低优先级的约束并使其成为我无法使用约束调整视图的大小。
由于您在 运行 时间设置高度,我会 select IB 中的 height/vertical 约束,您在 运行 时间不需要并转动 'Placeholder' 对于 'Remove at build time' 的约束,这应该会删除您的警告。
优先级设置决定了何时存在冲突约束将使用哪一个。
回答标题中的问题:降低约束的优先级告诉自动布局该约束不如所有具有更高优先级的约束重要。
这意味着如果两个约束冲突,自动版式将使用优先级最高的一个而忽略另一个。
如果两个 required 具有相同优先级的约束发生冲突,您将收到一条类似于您描述的错误消息。
我正在另一个视图中构建可变大小 table 视图。 table 视图不应滚动,因此我以编程方式确定其内容大小并调整高度限制,以便 table 视图始终适合其内容。
我 运行 遇到的问题是有关破坏约束的警告:
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 )>"
)
为了解决这个问题,我尝试了两件事:
- 设置 translatesAutoresizingMaskIntoConstraints = false。这会导致 table 视图出现问题。
- 将 adjustable 高度限制的优先级降低到 999。
第 2 点解决了我的问题,我可以使用较低优先级约束来调整视图的高度。但是,我不明白为什么会这样。
那么,自动布局如何解释约束的优先级?我本来希望 NSAutoresizingMaskLayoutConstraint 接管较低优先级的约束并使其成为我无法使用约束调整视图的大小。
由于您在 运行 时间设置高度,我会 select IB 中的 height/vertical 约束,您在 运行 时间不需要并转动 'Placeholder' 对于 'Remove at build time' 的约束,这应该会删除您的警告。
优先级设置决定了何时存在冲突约束将使用哪一个。
回答标题中的问题:降低约束的优先级告诉自动布局该约束不如所有具有更高优先级的约束重要。
这意味着如果两个约束冲突,自动版式将使用优先级最高的一个而忽略另一个。
如果两个 required 具有相同优先级的约束发生冲突,您将收到一条类似于您描述的错误消息。