Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Auto Layout: Align the edge of one button to the center of another button?

Tags:

ios

autolayout

So I have two UIButtons that I'd like to align, but not specifically the edges. I would like to align the (left) edge of one button to the (horizontal) center of another button. So I want it to look like this:

Button1

How would I do this AutoLayout in Interface Builder and/or programatically? I hope this isn't too much of a beginner question, but iOS AutoLayout can be so confusing at times...

like image 651
DKLA Avatar asked Oct 14 '14 02:10

DKLA


People also ask

What is margin constraints?

The “Constrain to margins” checkbox determines whether constraints to the superview use the superview's margins or its edges. The lower portion of the popover lets you set the item's width or height. The Width and Height constraints default to the current canvas size, though you can type in different values.

How do I add a Layout constraint in Xcode?

To create constraints select the button and click the Align icon in the auto layout menu. A popover menu will appear, check both “Horizontal in container” and “Vertically in container” options to center the button on the screen. Then click the “Add 2 Constraints” button. Run the application.

How do you add constraints in storyboard iOS?

Open the Align menu with the yellow button selected and check Horizontally in Container, then click Add 1 Constraint. Now, select both buttons at the same time using the Shift key and, in the Align menu, check Leading Edges. Again, actually install the constraint by clicking Add 1 Constraint.


1 Answers

In IB, create any horizontal constraint between the two buttons. For example, align their centers. Then, select the new constraint object and the Size inspector. There, you can change which attributes are aligned. Select the leading edge of the top button. Now the CenterX of the bottom button is aligned to the leading edge of the top button.

like image 189
Ken Thomases Avatar answered Oct 12 '22 15:10

Ken Thomases