I have 4 button in view and want each space between each button have the same adapt to screen iPhone. Any one can help please?
-
any suggestion please – sony Mar 05 '18 at 02:40
-
1use `UIStackView` – seto nugroho Mar 05 '18 at 02:42
-
@setonugroho can you explain how to use UIStackView? – sony Mar 05 '18 at 02:47
-
https://stackoverflow.com/questions/32862142/how-to-add-equal-spacing-and-equal-width-for-button-in-ios-auto-layout/32862693#32862693 – Bhavin Bhadani Mar 05 '18 at 12:45
1 Answers
You'll want to use UIStackView
for that.
Step 1:
Drag a button out, style it as you like, and make 3 more copies of it.
Step 2:
Drag them roughly where you want them. I'll assume you want 2 rows of 2 buttons.
Step 3:
Highlight the top two buttons and click the "Embed in Stack" button at the bottom of the screen (The down arrow one).
Do the same for the bottom two buttons. At this point, you should have two horizontal stacks. Set the desired spacing between the stacks and set distribution to "Fill Equally" in "Attributes Inspector".
Step 4: Highlight the top and bottom stacks and click "Embed in Stack". So now you've got a VERTICAL stack of horizontal stackviews. Set the desired spacing between the stacks and set distribution to "Fill Equally" in "Attributes Inspector".
Step 5: Highlight your stackview and click "Add New Constraints" button at the bottom of Interface Builder. (the square tie fighter thingie). You can do 0,0,0,0 for top, right, bottom, left with constrain to margins checked.
That's it. Here's a link to a repo I threw up.
When you're done, it'll look something like this:
Portrait:
Landscape:

- 16,233
- 18
- 112
- 180
-
-
@sony No idea what you're trying to do. Have a peek here. https://www.raywenderlich.com/160646/uistackview-tutorial-introducing-stack-views-2 – Adrian Mar 05 '18 at 13:02