How to equally space items in Xcode storyboard? - ios

I want to have 15 buttons equally spaced between them and correctly leading and trailing the superview vertically. I set constraints so that the left set of buttons aligns to the left margin and the right set aligns to the right margin. The first button leads from the top margin in each set, and the remaining buttons each have a constraint for vertical space set to the upper neighbor. I thought this would be enough to make it work but it wasn't. As you can see, for an iPhone 4, the number of buttons displayed on the screen is only 12 so I would definitely need to somehow dynamically change the button size depending on the screen size. I am attaching a screenshot from the storyboard that shows the Any x Any editor view and a preview for the 3.5-inch screen.

This was really easy to accomplish using stack views. Simply select all the buttons and click the leftmost icon (bottom right side of left pane) which is named Stack, and see the magic happening!

Try to set the constraints like this:
First select the all elements on left side and give leading constraint:
Now select first element and give top constraint:
Now select the second element and select it , and drag it to first element and give vertical spacing: , repeat giving vertical spacing for all the elements, So that each element will be vertical space constrained with its top element!!.
Do the same for elements on right side.
Give the trailing constraints for all elements on left,
and the select top element and give top constraint
and then select one element below top element and give vertical spacing between those two, repeat it for all the elements!!! So that each element will be vertical space constrained with its top element!!
Note: I only gave left constraints, you can try for right side too!!!!!

Add height constrain to the top left button to be 1/15 of the superview height and for all buttons let their height equal to the top left one.
Note it will work only if the vertical spacing is zero between buttons.
If you have a vertical space value for example x, then set it as 1/15 - 14 * x
Note #2 in iOS 9 only, seems the stack view will help perfectly in that

Related

How to use auto layout for a label in interface builder

How would I go about setting constraints for my price label to stay on the side of the screen? Below Is a picture of what I want my tableviews to look like on all iPhones.
http://imgur.com/a/g6PhL
It's easy to do:
1)first add left label
2)ctrl + drag the left label to cell content, and choose Center Vertically in Container:
3)Click Add New Constraints in the right bottom of interface builder,
deselect the Constraint to margins, and click the left dotted line and set the left margin value , eg:8
4) As the same to left label, you can set constraints to your right label, and the different is only you should set right margin nor left margin , in there I omit the Vertically in Container step of right label, you can see 2):
EDIT
as matt's advice, you better set the right label's alignment to right;
The blue label is on the right and needs to stay on the right. It is sized automatically by its content. So just pin its right edge to the right of its superview and its top or bottom (not both) to the top or bottom of its superview.

Autolayout constraint left

I'm just getting started with iOS development, and I'm following a series for making a game.
I want to align the circle (see screenshot) but there is no constraint for left
How can I make sure it aligns left for example on the iPhone 4 (see screenshot).
Xcode version (7.3.1 (7D1014))
Option 1: Hold control button, drag to the left and select `Leading spaces to container. The object that you are defining the space from should be highlighted. You should get the following menu:
Option 2:
1. Select the object
2. Press the pin button
3. Set the distance from the frame
4. Pressed the red button
5. Select Add constraints
Leading is left, and Trailing is right
First step, the most important one :
Remove that circle, and make sure your view has no constraint exception (look at the debugger) and that everything is where it should, on all phones. Ff that is correct, move on to the next steps.
Technically you should just be using Left or Leading, they're almost identical. (Leading is actually different, it'll align to the right in Arabic localized phones for example).
So what you wanna do is make sure your view has all its constraints set correctly, obviously, and for what I'm looking at you just need Top, Bottom Left and Right (or Leading and Trailing).
Just drag & drop from the circle to the view that is supposed to be the "anchor". For example, I'm guessing your top left circle will have its Left constraint going against the border of the view, and Top/Right/Bottom will be against the black bars. Easy.
For the left, drag & drop between the circle and the superview (the uppermost view in the hierarchy) and select Left/Leading. Then select the constraint and look at the properties.
Look at the image I have as an example. If you see the constraint properties on the right, I'm saying :
First item = "The leading side of my message box"
Relation = Should be equal (in distance)
Second Item = To the leading side of the Header label
Its plain english, the left side should align to the left side.
Your case is similar, you want the left side of your Image to be aligned with the left side of the View !
Now the 3 others constraints are sliiiiightly different, because you don't want Right-on-Right, Top-on-Top and Bottom-on-Bottom.
You actually need the Bottom of your circle to be aligned with the Top of the view under it, the Right of your circle to be aligned with the Left of the view on its right, and the top of the circle to be aligned with the bottom of the top bar.
Following?
Now you can adjust these constraints, you have a "Constant". This is the distance that the "Equal" refers to. If you say "the distance between this and that should be equal to zero", the two views will be sticking together. So usually you can add some margin.
Important note ! If you use the drop down for each Item (first and Second), you will see you can tick/untick margins ! This is important, it automatically adds Apple-advised margins, which is a value of 8, that is counted on top of the Constant value. So if you HAVE a margin and a constant of 0, you actually have a spacing of 8. This is to make your life easier because you'll usually have many margins and its easier to count from zero.
Any question?

Weird Autolayout Issue iOS

I'm trying to accomplish the following view:
So what I have is a view and 6 labels, the top two labels have both leading and trailing space to container of 0, the bottom left two labels have leading space to container of 0, the bottom right two have trailing space to container of 0, and I have a spacing of 12 in between the bottom left two and the bottom right two.
However, this is what I get when I run it:
How can I fix it so it looks more like the first picture?
Do you have an "equal width" constraint between the left labels and the right ones ? Without it, your "weird" issue is in fact totally right according to your constraint :
The left label tries to expand its width to the maximum possible. How can it ? By minimising the width of the right label, which is why it looks shrinked, but in the way its frame contains exactly the text "Label".
Select the left and right labels and add an "Equal widths" constraint in interface builder.

How to set equal horizontal spacing between UIfields

layouts and i have added 5 button on my view-controller and here i have applied constant width and height for all 5 fields
Here my main requirement is i want to set equal horizontal spacing between all fields and
For this i have fit for constraint For "middle button"
1)topspace
2)width
3)height
4)center horizontal in container
For "fist button"
1)topspace
2)width
3)height
4)leading space
For fifth button:-
1)topspace
2)trailing
3)width
4)height
For second and Fourth buttons i have given 1)top space,width,height,horizontal spacing but they are not setting perfectly please help me screen is coming like first image
but i want fit that screen like second image
Start out by setting each one of your views to have a constant width, let's say that it's 40. So every view now has a width of 40.
Now you need to place a leading/trailing constraint on your furthest left/right view to the superview (This is assuming you want the furthest left/right view to have a static distance from the left/right superview).
Now go ahead and create a horizontal constraint from your furthest left view to the second one in. Create a horizontal constraint from the second to the third. Then make a horizontal constraint from the furthest right view to the fourth. Now the fourth to the third.
What you should now do is edit the four horizontal constraints you have created by changing their value to not being "= constant", but ">= constant". Now you can lower that constant value to help adjust for smaller screen sizes as well. This setup should provide the result that you're looking for by having all of the views equally distanced, while letting the left and right view be closer to the sides.

How to put buttons at the bottom of the label?

I follow Developing iOS 8 Apps with Swift course. The calculator is being developed there. As the first step, I add label (display) with the following constraints -
right - trailing space to Superview
left - leading space to Superview
top - top space to Top Layout Guide.Bottom with value 8
Then I should add the button (digit 7 in my case) and put it in the left top corner, but below the label. But it doesn't work - when I take and move the button (or all buttons as shown on the screenshot below), then the left blue line indicating left constraint appears, but there is no line indicating bottom constraint of the label.
What is wrong there? How to place buttons exactly below the label?
First of all in your case you should embed all the buttons in a single view and then add constrains in that view relative to superview (be aware that now buttons should have constrains relative to newly created view). To do that select all the buttons go to Editor->Embed In->View. The created view cant have top-left and below display constrains in same time because top and below are conflicted constrains. So next add the left constrain for the view and vertical space between display and view with following rule : Display.Bottom equal View.Top with a constant of your value. Finally your 7 button should be placed top left of its superivew

Resources