简体   繁体   English

自动布局-添加多个约束的效果

[英]Auto Layout - effect of adding several constraints

I look over this article, and i have a question. 我看了这篇文章,我有一个问题。

Consider following constraints on text field object: 考虑对文本字段对象的以下约束:

First Name Text Field.Top >= Top Layout Guide.Bottom + 20.0
First Name Text Field.Top = Top Layout Guide.Bottom + 20.0 (Priority 249)

I'm confused. 我糊涂了。 If i add greater then or equal constraint, isnt that mean that i already say - set this distance greater then or equal 20? 如果我加上大于或等于约束,那是否就意味着我已经说过了-将此距离设置为大于或等于20? What the point to add second constraint - First Name Text Field.Top = Top Layout Guide.Bottom + 20.0 (Priority 249) . 添加第二个约束的意义是什么First Name Text Field.Top = Top Layout Guide.Bottom + 20.0 (Priority 249) It also have lowered priority, so i suppose, it will never evaluated, because first constraint - First Name Text Field.Top >= Top Layout Guide.Bottom + 20.0 have greater priority. 它的优先级也降低了,所以我想它永远也不会评估,因为First Name Text Field.Top >= Top Layout Guide.Bottom + 20.0具有更高的优先级。

Could someone explain that logic to me? 有人可以向我解释这种逻辑吗? Is there any point to add 2 constraint with different priorities? 是否有必要添加2个具有不同优先级的约束? Any point to add greater-then-or-equal constraint and constraint, that equal to same value (20 in case)? 有什么要增加等于或大于等于的约束和等于相同值的约束(如果为20)? Thanks. 谢谢。

A greater than or equal constraint is ambiguous by its very nature. 大于或等于约束就其本质而言是模棱两可的。 You have told Auto Layout to make the distance between your first name text field and the top layout guide's bottom at least 20 . 您已告诉自动版式 ,将您的名字文本字段与顶部版式指南的底部之间的距离至少保持 20 Note that 20 , 30 , and 50 would all satisfy that constraint. 需要注意的是203050将所有满足该约束。

By adding the second constraint, you tell Auto Layout that you'd prefer it to be 20 . 通过添加第二个约束,您可以告诉Auto Layout您希望它为20 By making the priority 249 you tell Auto Layout that this request is low (priorities go to 1000 ) and should be broken first if it needs to break a constraint to make things work. 通过将优先级设置为249您可以告诉Auto Layout这个请求很低 (优先级为1000 ),如果需要打破约束以使其正常工作,则应首先将其中断。

So, using these 2 constraints, Auto Layout will make the distance 20 if it can. 因此,使用这两个约束, 自动布局将使距离为20 If not, it will use a value greater than 20 . 如果不是,它将使用大于20的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM