How to Add Equal Spacing and Equal Width for Button in iOS Auto Layout

How to add Equal Spacing and equal width for button in iOS Auto layout

check this image and made your constraints like below...

Sample Image

RESULT:- preview in different sizes

Sample Image


With Stackview (For iOS 9.0 and above)

Sample Image

NOTE: If you have to make app for iOS 9 and later then UIStackView is another option for you

Xcode 7 - Constraints - equal spacing between buttons

To layout 11 buttons equally spaced, put 10 spacer views between them:

11 buttons


Set the following constraints:

  1. Set a Leading Space to Container View Margin constraint between the leftmost button and the left margin.
  2. Set a Trailing Space to Container View Margin constraint between the rightmost button and the right margin.
  3. Pin all of the buttons and spacer views to their left and right neighbors. The constant should be 0.
  4. Set the height of all of the spacer views (to something like 64).
  5. Set an Equal Widths constraint between the leftmost button and the top level view. Change the multiplier to 1:18. This will allow the button size to grow on bigger screens.
  6. Select all 11 buttons (by holding down shift and click on the 11 buttons. Choose Equal Widths and Aspect Ratio. Make the Aspect Ratio constant 1 for square buttons.
  7. Select all 10 spacer views and set Equal Widths.
  8. Set a Vertical Spacing to Top Layout Guide constraint between the leftmost button and the top margin.
  9. Select all 11 buttons and 10 spacer views and select Align Vertical Centers.
  10. Select all 10 spacer views and set their hidden property in the Attributes Inspector.

On the iPhone 4S (3.5 inch):

iPhone 4S Simulator


On the iPhone 6S Plus (5.5 inch):

Sample Image

Autolayout with equal width and setting priority

iOS9 only apps:

This might be a good time to use the new Horizontal Stack View. Just set distribution to fill equally and alignment to fill

For iOS8 support:

You'll have to set the width constraint to equal width between the components and the horizontal spacing to 0

Set button1 to equal width with button2 then button1 to equal width with button3. This should be enough.

iOS Autolayout: two buttons of equal width, side by side

Add the following constraints

  1. Assign equal width from button1 to button2.
  2. Assign horizontal spacing between both buttons.
  3. Assign leading space from button1 to its superview.
  4. Assign trailing space from button2 to its superview.
  5. Assign top space to both the buttons.

    Let me know if it works for you.

set UIButtons equal distances apart with autolayout

Autolayout doesn't support this right out of the box.

Luckily there is a clever way how to do this. Insert transparent UIViews between the buttons. Mark these UIView's widths equal to each other. Now they will take equal spaces between each other.

This is how it would look. Portrait.
Sample Image

Need assistance setting two buttons of equal width, side by side Autolayout

you can done it using equal width to 2nd button like this

Sample Image

and the output is

Sample Image

Here is apple guide



Related Topics



Leave a reply



Submit