自动布局:Y位置为两个值的最大值 [英] Auto Layout: Y Position as the Max of Two Values

查看:80
本文介绍了自动布局:Y位置为两个值的最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮playButton和两个UIView,即myView1和myView2,它们的位置在执行过程中可能会发生变化.

I have a button playButton and two UIViews, myView1 and myView2, whose positions may change during execution.

我希望playButton的顶部比UIView1的底部或UIView2的底部低10个单位,较大的值(进一步向下).

I want the top of playButton to be 10 units below the bottom of UIView1 OR the bottom of UIView2, whichever is the larger value (further down).

我该如何在代码中使用自动布局表达这一点?我尝试将一个约束设置为更大或相等,但似乎没有效果.

How would I express this using auto layout in code? I tried setting one constraint as greater or equal but it seemed to have no effect.

推荐答案

这是一种思考方法:创建一个约束,使playButton的顶部大于或等于myView1的底部加10,这是另一个约束playButton的顶部大于或等于myView2的底部加10,然后是第三个约束,即playButton的顶部以低优先级位于共享超级视图的顶部.

Here's one way to think about it: create a constraint that the top of playButton is greater than or equal to the bottom of myView1 plus 10, another constraint that the top of playButton is greater than or equal to the bottom of myView2 plus 10, and then a third constraint that the top of playButton be at the top of the shared superview at a low priority.

两个不等式将确保按钮在两个视图下方.但是,这就产生了歧义.该按钮可能在两个下方的任意位置.这样就不能满足第三个约束条件,但是自动布局系统将尝试尽可能地接近.这解决了歧义.该按钮将尽可能靠近顶部,同时仍在两个视图下方.

The two inequalities will make sure the button is below the two views. However, that leaves ambiguity. The button could be anywhere below both. The third constraint can't be satisfied as such, but the auto layout system will try to get as close as possible. This resolves the ambiguity. The button will be as close to the top as possible while still being below both views.

这实际上可以简化.您可以将不等式之一与低优先级等式组合在一起.有一个约束条件,即playButton的顶部大于或等于myView1的底部加10.具有第二个约束条件,即playButton的顶部等于 myView2的底部加10,但在优先级较低.

This can actually be simplified. You could sort of combine one of the inequalities with the low-priority equality. Have one constraint that the top of playButton is greater than or equal to the bottom of myView1 plus 10. Have a second constraint that the top of playButton is equal to the bottom of myView2 plus 10, but at a lower priority.

如果myView1的底部低于myView2的底部,则第一个约束条件要求playButton低于其底部.第二个约束无法满足,但系统会尝试尽可能靠近myView2的底部.这样可以使按钮尽可能高,同时仍位于myView1的底部下方.如果myView2的底部低于myView1的底部,则第二个约束条件将直接确定按钮的位置.第一个约束也得到满足,因为它是一个不等式.

If myView1's bottom is lower than myView2's, then the first constraint requires that playButton be lower than it. The second constraint can't be satisfied, but the system tries to get as close as possible to the bottom of myView2. That keeps the button as high as possible while still being below myView1's bottom. If myView2's bottom is lower than myView1's, then the second constraint determines the position of the button directly. The first constraint is satisfied, too, because it's an inequality.

这篇关于自动布局:Y位置为两个值的最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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