Piepants Piepants - 1 month ago 4x
iOS Question

How to constraint two buttons to be equidistant from the vertical center

I'm fairly experienced with using constraints, but up till now everthing I've arranged has been aligned along some vertical or horizonal center.
I've spent all morning sifting through past questions and tutorials but am still unable to arrange these two buttons, it ought to be simple, but I can't find the vital missing piece

enter image description here

XCode's own suggested constraints are just off their head on drugs, and trying to add them myself where it is going wrong is if I set all the constraints up for one screen size, then when viewing them in another they are not centered:

enter image description here

How do I add a constraint so that the buttons are aligned on either side of the vertical center?


@Gereon's answer is better than @conarch's, IMHO, because spacer views are almost never the right approach. I have a third approach, though, that I find to be more robust than @Gereon's because it doesn't rely on hard-coded constants.

  1. Enclose both buttons in a UIView (Editor -> Embed In -> View).
  2. Constrain the left button's left edge to the left edge of the parent view.
  3. Constrain the right button's right edge to the right edge of the parent view.
  4. Constrain the left button's right edge to the right button's left edge and give it the desired spacing constant.
  5. Constrain the tops and bottoms and/or heights as you see fit.
  6. Align the horizontal center of the parent view to its parent view's horizontal center.