Technicolor
Technicolor

Reputation: 1619

Xcode - Using constraints to place items not exactly horizontally centered

In my storyboard, I have two buttons at the bottom of the view. Neither of them are in the middle. When I run the program on devices like the iPhone 5s, the first button is aligned to the left where I'd like some space between them. I've tried using a leading and trailing space constraint for the first and second buttons respectively (the two have a trailing/leading constraint between each other), but this then causes the buttons to go off-screen. This is how the view looks on the storyboard, which is what I'd like it to look like:

enter image description here

How do I make sure that both buttons stay on the screen w/ proportional distances between each other and to the sides of the screen?

Upvotes: 0

Views: 72

Answers (1)

NKorotkov
NKorotkov

Reputation: 3681

After you have added bottom constraints to your buttons select them both and set X centered in container:

enter image description here

After that, select each button individually and in Size Inspector find this "align X center" constraints and set offset by changing the constant. Say you want them to be 100 points apart. Set constant 50 pts for one of them and -50 pts for the second one.

enter image description here

This is how it's going to look:

enter image description here

And that's it! Only 2 constraints for each button needed. Here's how it will look on the device:

enter image description here

Upvotes: 1

Related Questions