降低约束的优先级到底有什么作用? [英] What exactly does lowering the priority of a constraint do?

查看:70
本文介绍了降低约束的优先级到底有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am constructing a variable size table view within another view. The table view should not scroll so I am programmatically determining its content size and adjusting a height constraint so that the table view always fits its content.

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

The problem I run into is a warning about a broken constraint:

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 )>"
)

为解决此问题,我尝试了两件事:

To fix this, I tried 2 things:

  1. 设置translationsAutoresizingMaskIntoConstraints = false.这会导致一切都变得与表格视图无关.
  2. 将可调高度约束的优先级降低到999.

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

Point 2 solves my problem and I'm able to adjust the height of the view using the lower priority constraint. But, I don't understand why this works.

那么,自动布局"如何解释约束的优先级?我本来希望NSAutoresizingMaskLayoutConstraint能够接管较低优先级的约束,并做到这一点,所以我无法使用该约束来调整视图的大小.

So, how does Auto Layout interpret the priority of a constraint? I would've expected the NSAutoresizingMaskLayoutConstraint to take over the lower priority constraint and make it so I could not resize the view using the constraint.

推荐答案

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

To answer the question in the headline: Lowering the priority of a constraint tells the autolayout that the constraint is less important than all constraints with a higher priority.

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

This means that if two constraints are conflicting autolayout will use the one with the highest priority and disregard the other.

如果两个具有相同优先级的必需约束发生冲突,您将收到一条与您所描述的错误消息类似的消息.

If two required constraints with identical priorities conflict, you will have an error message like the one you describe.

这篇关于降低约束的优先级到底有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆