iOS: How to Align The Center of a View With The Bottom of Another View With AutoLayout

前端 未结 6 665
离开以前
离开以前 2021-02-02 15:52

I want to make the center of the head imageView with the bottom of the blue title imageView\'s keep alignment.

How can I do it by using Autolayout?

screen 4.0inc

6条回答
  •  -上瘾入骨i
    2021-02-02 16:24

    Step 1: Create your views as shown below.

    Step 2: Set background view constraints to all three edges.

    Step 3: Set your own ratio. Since, you mentioned that you don't have known height.

    Step 4: Select your center view and add width & height constraints as you want.

    Step 5: Set center view as horizontal center in the superview as shown below

    Step 6: Now select both background view & center view. Set align constraint to "Vertical centers".

    Step 7: Now, you can see a constraint with a warning with wrong constant like below. Select that constraint

    Step 8: In the Connections inspector, select the "second item" and change it to "bottom"

    Step 9: That's it. If there is a warning, update your constraints. You can check in different simulator with any screen sizes.

    You don't need any super views or new sibling to accomplish this. Easy way to make any view to center and keep it as bottom/top of another view.

提交回复
热议问题