Automatic layout: Y Position as a maximum of two values

I have a playButton and UIViews, myView1 and myView2, which positions can change at runtime. I want the top of the playButton to be 10 units below the bottom of UIView1 or the bottom of UIView2, whichever is greater (further down) . How can I express this with auto layout in code? I tried to set one constraint both greater and equal, but it doesn't seem to have any effect.

+3


source to share


2 answers


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

The two inequalities will ensure that the button is below the two views. However, this leaves ambiguity. The button can be anywhere below both. The third constraint cannot be met as such, but the auto-layout system will try to get as close as possible. This removes the ambiguity. The button will stay as close to the top as possible while still under both views.



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

If the bottom of myView1 is less than myView2, then the first constraint requires the playButton to be lower than it. The second constraint cannot be met, but the system is trying to get as close to the bottom of myView2 as possible. This keeps the button as high as possible but below the bottom of myView1. If the bottom of myView2 is lower than myView1, then the second constraint determines the position of the button directly. The first constraint also holds because it is an inequality.

+7


source


Illustration of KenThomases answer:

view1-> Blue

view 2-> red

view 3-> pink

The constraint between view1 and view 3: Greater than or equal to 20 with a priority of 1000.

Restriction between view2 and view 3: Equal to 20 with priority 999.



The work is explained by Ken in his answer. Just take a look.

If view 1 is greater than view 2

enter image description here

If view 2 is greater than view 1

enter image description here

+2


source







All Articles