Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I centre/center multiple objects vertically and horizontally in XCode's interface builder?

So I've set up my constraints:

enter image description here

In each device these objects are displaying as expected.

iPhone:

enter image description here

iPad: enter image description here

But not I'd like to control how high up or low down these objects are from the top or bottom of the screen or just centre them. You can see from my screen shot they're not centred horizontally.

What I've tried:

When I try to centre them I keep getting those annoying yellow warnings. I even tried to add a view and put the objects inside the view and setup the constraints then centre that view. I still got errors.

How do I do this correctly?

like image 433
LondonGuy Avatar asked Sep 01 '14 19:09

LondonGuy


1 Answers

Had a bit more time so i thought I would explain it a little better

1) Select the button / thing you want to position, in my image below it's a button called actions which I want to place to the right someway down the screen

enter image description here

2) click align from the buttons at the bottom right

enter image description here

3) choose align "horizontal centre in container" or vertical depending on whether you want to position it vertically or horizontally

enter image description here

The screen will look something like this, ignore all the complaints in yellow we will fix them later

enter image description here

4) Choose the newly created constraint from the buttons "size inspector" on the right, select it and edit it

enter image description here

5) At the top choose the "first item" and select "reverse first and second item" from the pull down menu

enter image description here

6) Then choose what part of the button you want to align ie leading edge / middle so far the right side of xcode should look something like this:

enter image description here

the super view has to still be:

superview.centre X

7) then you can set the multiplier to divide the screen equally or to a percent, ie 0.5 / 1 / 1.5 - quarter way across screen, halfway, 3/4 way across

enter image description here

You see the number on the yellow constraint has changed

enter image description here

If you change the multiplier again

enter image description here

The number will change

enter image description here

Don't worry that the button hasn't moved, that all happens when you update the view select the main view and then select "update all frames in view" from the bottom right update menu

enter image description here

Don't worry if everything else on the screen goes wrong whats important is your button is positioned correctly in this case horizontally in the view (you can at this point do an undo and go back to the point before the update, make a change to the constraint and then update again, then undo and tweak and go round like this until you have it perfect)

enter image description here

Now I will go back and apply the same method to the vertical alignment and then the same to each button in the view

The important thing is you can set your button position however you want, center / leading etc but you MUST leave the alignment to be the center of the superview if you change it to top or leading it will convert the constraint so only constants work not multipliers

EDIT: To add constraints you use the red box to constrain the thing to expand and contract to the edges of the device, if you simply want to fix the size of the thing you can use the constraints in the yellow boxes, remember to click the add constraints button at the bottom enter image description here

like image 119
Gordon Worswick Avatar answered Sep 28 '22 07:09

Gordon Worswick