Search code examples
iosswiftuiviewautolayout

How to set a view on top of two views using Autolayout iOS


I am trying to make a layout, where one view suppose to be on top of two views, in center that is half of first view and other half on second view's half.

I am not able to understand what constraint can i give for that. And is it possible to do it using attribute inspector or have to do it programatically.

My black view is equal width and equal height to white view with 0.75 multiplier. and x position is 0. Y position is what, i am not able to set.


Solution

  • constraint black view's centerY to top white view's bottom or bottom white view's top. If those two white views touch and will always meet at the center of the black view, this will work.

    To do this in Interface Builder, ctrl+drag from black view to view one of the views and select center vertically. This will create constraint that ties black view's center to white view's center. This is not what we want yet, but we change that easily. Select this new constraint and under "Second item" select Bottom instead of Center Y, set constant to 0 and you are done

    enter image description here