1

I am in a need of having the series of buttons to be evenly placed in superview Horizontally using Auto Layout. Here, I want to keep the sizes of the subviews same, only the center of the subviews will be placed in such a way that there is equal number of space between them.

Note: I dont want to set the Size of the superview, I want every thing to be Auto Layout-ed.

Please Help, I am stuck !!

Thanks!!

infiniteLoop
  • 2,135
  • 1
  • 25
  • 29

1 Answers1

0

You can create as many UIView's as you have buttons, and center the buttons inside the views, the views can be aligned back to back, using this code:

[constraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view1][view2][view3]|"
                                                                         options:0
                                                                         metrics:nil
                                                                           views:views]];

Make sure you first remove existing constraints from the superview using:

[self.view removeConstraints:self.view.constraints];

and in the viewDidLoad turn off auto resizing conversion:

[self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
Whakkee
  • 1,867
  • 1
  • 16
  • 18
  • thanks for the reply !! but here I dont want the views to be placed one after the other (leading-trailing) but should be spaced in such a way that they appear evenly distributed along the width with common gap between them. – infiniteLoop Apr 23 '13 at 21:25
  • Yes, that's why you center the buttons inside the views, that way they are evenly distributed. The views are back to back, and the buttons are centered inside them. – Whakkee Apr 24 '13 at 06:39
  • Ya I was doing the same way as you suggested as a work around. – infiniteLoop May 07 '13 at 19:30
  • But I guess there has to be some way of achieving it without the extra views, in Auto Layout environment. – infiniteLoop May 07 '13 at 19:32
  • But this is going to use a standard margins between views, right? How would you make it custom? – Javier Soto Aug 05 '13 at 20:26