-1

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?

sony
  • 730
  • 2
  • 9
  • 19

1 Answers1

2

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).

enter image description here

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".

enter image description here

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".

enter image description here

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.

enter image description here

That's it. Here's a link to a repo I threw up.

When you're done, it'll look something like this:

Portrait:

enter image description here

Landscape:

enter image description here

Adrian
  • 16,233
  • 18
  • 112
  • 180