multitaskPro
multitaskPro

Reputation: 571

Use auto layout to position button near the center

I'm using auto layout in Xcode and I have some buttons I want to be near the center, but not on it. I know I can use alignment constraints to be exactly in the center, in this menu: enter image description here

However, I want to not be exactly in the center, lets say 50 pixels up and to the right. How can I do this? I'm sure it's an easy thing to do, but I can't seem to find anything elsewhere on the web.

Upvotes: 0

Views: 285

Answers (2)

Bringo
Bringo

Reputation: 610

If you want the center of your button to be 50 points up and to the right of the parent view's center you can use a center x alignment constraint and a center y alignment constraint and set the center x alignment constraint's constant to 50 and the center y alignment constraint's constant to -50.

off center by 50 on each axis enter image description here

Upvotes: 2

Luca Angeletti
Luca Angeletti

Reputation: 59496

Create a view with size 50 * 50. Then place this view exactly at the center using auto layout.

enter image description here

Next you need to place your button using vertical space and horizontal space in order to be aligned to the upper right corner of the view.

enter image description here

Finally make the view transparent.

enter image description here

Upvotes: 0

Related Questions