13

My design requires that a button is centered between two other objects. One of the objects is in the vertical center of the view (green). The other object is some distance from the bottom edge (green). Now the task is to center the third object (red) between the other two. I am using xcode6's new constraints and my view is in wRegular hRegular mode. This would be easy with code, but I am trying to use the storyboard to accomplish this.

enter image description here

mfaani
  • 33,269
  • 19
  • 164
  • 293
Joe Andolina
  • 648
  • 1
  • 11
  • 23

2 Answers2

17

There are a number of approaches:

  • In iOS 9, the easiest would be to define a vertical UIStackedView with a distribution of "equal spacing" and then addArrangedSubview the three circular subviews

  • Another option in iOS 9 would be to create two UILayoutGuide (which would represent the two question marks in your image), add them to the shared superview with addLayoutGuide and define them to be the same size as each other. The corresponding VFL might look like:

    "V:|[greenView1(==100)]-[layoutGuide1]-[redView(==50)]-[layoutGuide2(==layoutGuide1)]-[greenView2(==greenView1)]|"
    
  • In earlier iOS versions, rather than using UILayoutGuide, you could just create two "space" views (UIView with clear background so they're not visible) then define a constraint such that their heights are identical, and then define vertical spacing constraints between the five views (the three circles and the two spacer views) with a constant of zero.

    It might look like:

    IB scene

    I've made those "spacer" views visible, to illustrate the idea, but obviously you'd set them to be transparent so you can't see them in the UI.

    This is logically equivalent to the UILayoutGuide approach in iOS 9, except that the UIView "spacer" views just carry a little more overhead than a UILayoutGuide. But in iOS versions before 9, this is the common approach to this problem.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • 5
    You can do it with one spacer. Pin the top of the spacer to the bottom of the upper green circle. Pin the bottom of the spacer to the top of the lower green circle. Pin the vertical center of the red circle to the vertical center of the spacer. – rob mayoff Sep 17 '14 at 19:20
  • Agreed. Lots of ways of skinning the cat. I use individual spacers so that it works with more than three circles without dealing with multipliers (though you can now do that, too). – Rob Sep 17 '14 at 19:22
  • Both great solutions! I like that in the first solution the red button doesn't need to be a child of the spacer views. Thank you. – Joe Andolina Sep 18 '14 at 02:50
  • FYI, for the benefit of future readers, I've updated my answer to reflect two new approaches available in iOS 9, namely `UIStackedView` and `UILayoutGuide`. – Rob Jan 31 '16 at 21:30
1

I've come up with easiest approach.

newly added view is in blue color. and in my case label should be in center

Just follow these steps :

  1. add a view between them the two circle
  2. add its trailing and leading space. (in my case zero to superview)
  3. add top and bottom space from the circles.
  4. now put your red circle in the recently added view
  5. make it horizontally and vertically center from Alrignment Menu

alignment

Talha Ahmad Khan
  • 3,416
  • 5
  • 23
  • 38