0

I want the same space between my six buttons shows in the picture

I want the same space between my six buttons shows in the picture, I'm having problems with autolyout, do you know how can I put them with the same equal spacing? all of them have the same dimension, widht and height: 36

They are in an UIView and the UIView is inside UITableViewController like you can see in the picture

  • if your app is supporting to only iOS 9 and above then you stackView. its better option for you in this situation. If you want how to do it let me know will post answer – Pravin Tate Dec 30 '15 at 17:43
  • @PravinTate is for ios 8.1 and above. – Santiago Carmona González Dec 30 '15 at 17:50
  • so how much spacing you want to set in those buttons ? – Pravin Tate Dec 30 '15 at 17:51
  • the spacing must be porportional to device screen, I only want that they look symmetrical @PravinTate – Santiago Carmona González Dec 30 '15 at 17:53
  • there are two way for it. one is increase button size accordingly device frame this way is recommended by apple, because if device size is increase then apply say you must be change buttons size accordingly. other way is add other view between in that and then set constraint because there is no any way to set equal spacing in views it is possible with only height and width. for adding view between you buttons you can refer this link [enter link description here][1] [1]: https://www.packtpub.com/books/content/evenly-spaced-views-auto-layout-ios – Pravin Tate Dec 30 '15 at 18:14
  • I prefer this way of doing this: http://stackoverflow.com/questions/13075415/evenly-space-multiple-views-within-a-container-view/25898949#25898949 Fairly easy and evenly distributes your views. – Rob Booth Dec 30 '15 at 22:21

5 Answers5

1

Use a stack view if you can.

Otherwise place views in between each button and set them to have equal width to each other.

Wain
  • 118,658
  • 15
  • 128
  • 151
1

As all the icons are of same size, you can manage this through code which is generic to all device resolutions. A small calculation would help.

In this case,

              let x = screenwidth (or UIView) 
                  y = width of the icon (36 here)
                  a = space between superview and first icon (Leading Space)
                  b = space between last icon and super view (Trailing space)

First add width and height constraints to all the icons so that the width is constant.

Now add horizontal spacing constraints from 2nd button to 1st button (d), 3rd button button to 2nd button(d), 4th button to 3rd button(d) and so on till 6th button to 5th button(d) and give these constraints.constants the below value programatically by creating nslayoutconstraint variables.

As the spacing should be same, all these spacing constraints will equal to on evalue, say d.

d + d + d + d + d = (x - 6y - a - b)
5d = (x - 6y - a - b)
d = (x - 6y - a - b)/5
Robert
  • 5,278
  • 43
  • 65
  • 115
0

There are two ways, 1. Insert views between the buttons, that should be proportional to the superview and keep the view widths same. 2. Add constraints for every button centre to superview centre and put the multipliers as to get the same distance from centre.

tausun
  • 2,154
  • 2
  • 24
  • 36
0

use UICollectionView, and maintain cell spacing correctly...set horizontal scroll only.It looks perfectly, but it consumes extra memory..

phani
  • 105
  • 2
  • 15
-1
  1. If you are using iOS 9+ use storyboard it is best solution for these type of situation
  2. If you are using below iOS 9 then use following solution, check link enter link description here

Because there is no another solution for set equal space between objects, so you need to create spaceView for that. Just follow that link and you will get what you want. I have personally tried that thing and it work for me. So try it.

If you have any doubt let me know I will explain it.

I hope it will help you!

Community
  • 1
  • 1
Pravin Tate
  • 1,145
  • 8
  • 18