Reputation: 430
I am trying to get into the Auto Layout business, but i find it kinda hard.
I am trying to get 5 image views to display next to each other in the center of the view. They need to resize themselves to expand their height / width as well.
This is how it looks in IB (and kinda the way it needs to look when running the app):
So i have the following constraints:
However, when i run it, it looks like this:
And i am just kinda stumped here. What am i doing wrong?
Thanks in advance, Best Regards - /JBJ
** EDIT ** I added a trailing constraint to the last button. This makes sure they are all within the screen, but is kinda problematic when thinking about the size of it, so that didn't solve it either.
* EDIT EDIT * Tried removing the top and bottom constraint since they are the ones forcing the size up. Added a vertical center constraint to them all. This won't work either. Displaying them very small (lined up nicely, but too small) and also comes with warnings:
Upvotes: 6
Views: 5672
Reputation: 134
Place 5 buttons vertically and horizontally center in UIView
Select all of them and embed them in a stack view
Change distribution to fill equally.
Adjust spacing in attribute inspector to make space between buttons.
Add leading and trailing space and height constraint to stack view. Also vertically align it.
Upvotes: 0
Reputation: 77621
OK, here goes...
Make sure you update the frames once you're done to reposition the buttons to their new constraints...
The preview screen shows this working at all different sizes...
Upvotes: 21
Reputation: 2469
What I always do in such a situation is just simply think. How much constraints do I need to 100% define the design? What should I write to tell someone on the phone what it looks like?
In your case this are the following constraints (hope I wont forget one)
Upvotes: 0
Reputation: 944
For placing them at the centre of the screen vertically use
NSLayoutConstraint *constraintHorizontal = [NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.superview
attribute:attribute
multiplier:1.0f
constant:0.0f];
For placing them horizontally Button width = (width of the screen)-(button spacing dimension)- (left distance)- (right distance)/5;
Same for height. Initial left constraint for the first image view will be left distance.
Upvotes: 1