How to set equal horizontal spacing between UIfields - ios

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.

Related

How do I fix layout issues with constraints in Xcode?

I used the following constraints to build one screen for my app. However the icon buttons get messed up in different screen sizes (screenshots below). Is there a way to rectify this?
1- Give equal heights and equal widths to all 4 boxes.
2 - For first one-> leading, top,
- For second one(travel and place)-> horizontal spacing to 1st one,leading and top
- For third one(activity)-> vertical spacing with 1st one ,horizontal to utilities and leading
- For fourth one-> trailing , vertical spacing to 2nd one and horizontal spacing to activity
What I would do is:
Set a proportional size (width and height) to the first button, maybe 40% of the container.
Then for the other 3 buttons, you can set them to the same size as the first.
Add some space between them.
And thats it.
1 - Take two views as two rows
2 - Let for first view (row), add two subviews , give the width of first equal to width of superview and multiplier 0.5 , let the second view align leading to first view trailing and width equal to first view
3 - add the button as subview in each view with top , right , bottom and left spacing as desired. Repeat the same for 2nd button in 2nd view.
4 - Repeat the above steps for row 2 view
hope this helps
These are actually pretty straightforward when you think about how all the views are supposed to relate to each other. I made a demo storyboard to show you how I'd wire it up. The key things:
Each box has a 1:1 aspect ratio.
Boxes in a row have equal widths to each other
Each box is constrained on both sides to the edge of the view or each other
The icon's height is set to a % of the box's height
The labels are pinned to the top, right, and left of the box
Obviously you'll want to adjust your styling as appropriate, but that should get you the effect you want.

Q: Auto Layout 4 squares Issue/General Auto Layout Issue

So I'm just trying to wrap my head around Auto Layout; I understand how to use constraints, just not how to apply them appropriately. In the picture below I had set no constraints and simply want this simple design to resize correctly on all size classes (Horizontally and Vertically).
http://tinypic.com/r/2ymxbop/9
What constraints would I need to put in to have these squares resize appropriately on all screens (horizontal/vertical) and can you do this in the ( W:Any H:Any ) size class or do you have to set constraints for each individual size class?
Thanks!
One size class is all you need in this case.
Set a constraint for each of the 4 gaps between the rects (i.e. the space between the top 2, between the top-left and bottom-left, ...).
Set a constraint for each rect to its 2 closest outer edges.
Now you've set four constraints for each rect - and that's all you need!
For that case: follow the steps
1.Add four uiview to screen, two top and two bottom.
2.In your case the view not touching to bottom layout guide,so you have to give fixed height.
Start the add constraints to views.
All views are correct width and height.
3.So take first view from left top,give leading space ,top space and adjecent spacing to right top view. And select the pin button from the canvas below right side option availabe, select height ,give how much height you need.
4.Now select right top view ,give constraint to top layout guide and trailing space.and also same height what you selected before for view one.
5.Now view three ,left bottom view , give leading ,spacing to top view and right spacing to fourth view and select same height .
6.Now fourth view, give spacing to top view ,trailing to container and select height.
Almost completed.
7.Select all views at once ,give equal withs(option available in pin).
lastly some warning will shows ,do update frames.

Using Autolayout adjust vertical position from horizontal center if too high

In the diagram I need to center align the right view with the left view. If the right view is larger than the left view I need to top align it. Can I do this with constraints (storyboard) or do I need to do something in code to adjust the position (via a constraint) of the right view after the size has been determined? The Right view has multiple UILabels that can grow so I don't know the size until runtime. The container is actually a UITableViewCell.
Centering is easy. The right view resizes and the cell adjusts to that size. The issue is the requirement to top align if its too big.
You want this:
You can do it with two constraints.
You want the top of the green view to be greater than or equal to the top of the pink view, always. That is, you require that green.top ≥ pink.top.
In addition, you want the Y-center of the green view to be equal to the Y-center of the pink view, when possible. That is, you prefer that green.centerY == pink.centerY, if it doesn't violate the other constraint.
Here's the first constraint, in the storyboard editor:
And here's the second constraint:
Note that I have lowered the second constraint's priority to 800, which means it's not required but it's strongly preferred.
You don't need to write any code to make this work. I did write code in my demo to wire up the slider and the label, but I didn't have to modify those two constraints from code.
In the storyboard, align the right view to the left view top.
Give the value as
topAlignCons.value= +(leftView.height/2) //in storyboard
so that it gets align to the center of left view
Create an outlet for the top align constraint in your view controller.
When the right view is larger that left view, change the outlet constraint value to 0.
topAlignConstraint.constant=#0;
Now it will be aligned to the left view top with no negative value.

Dividing screen in to 1/3 and 2/3 views using autolayout

Is there a way to divide the screen by percentage (not equal percentage)
I need to split the screen to two parts 1/3 and 2/3.
I know how to split to equal parts but cant figure out how to split to non equal.
Thanks
Make an "Equal widths" constraints between your view and the super view with multiplier 2:3 for one view and 1:3 for the other. See picture. "Equal height" if you want to split in the other direction.
Steps :
Drag a Two UIView objects on viewcontroller.
Pin left-view to left, top and bottom of superview
And Right-view to right, top and bottom of superview.
Pin the horizontal spacing between two views(left and right). Make sure constant is zero since/if you want no spacing.
Select left-view and superview and set width equally (hold your patience). Now go to size inspector and edit width equally constraint having left view selected. Now set the multiplier to 0.33 (now % width is set).
See the result :
Using storyboard & autolayout it's very easy: I suppose you need to split the screen vertically (but horizontally it's the same trick).
So using a single view controller, drag&drop two views and set them attached to top and bottom. For the the view 1:
view 2:
Now you need to set the constraint equal height for the view 1 among the main view and the same for the view 2. After that you can edit these constraints settings a value (between 0 and 1) as multiplier. Double click on the constraint:
and set the value that you want for proportion (the trick it's maintain the sum of these constraints as 1 or just put in "2:3" here and "1:3" in the other view):
So this is the result:
I hope this is what you need, tell me if you need more details!
You can use the Equal Widths constraint as in the accepted answer, however, if what you're trying to constrain is not two sizes but rather one point to another, such as constraining one view's horizontal center to the 1/3 or 2/3's point on the screen, then it won't work because you can't constrain a point in space to a size.
But if you know say, view1, in your xib is going to be the full width of the screen, then, for example with the horizontal center point, you can constrain it to that view's trailing edge, and if you want the view to be centered at 1/3 or 2/3's the screen's width then set the multiplier to 1:3 or 2:3, respectively.

Evenly spread buttons horizontally across screen using autolayout. Not equal spacing between them

I need to layout a series of buttons horizontally across the screen so that they're equally spaced across the screen, not equally spaced between themselves. An example is having 3 buttons there they are evenly spaced so that the first button is centered at 25% of the screen width and others are 50%, 75% of the screen width.
This is relatively straightforward manually placing them in x,y coordinates but I'm trying to avoid mixing approaches.
The prevailing recommendation is to use spacers (UIView) between the buttons and put a constraint to make the spacers equal. This does not work if the buttons being spaced are of potentially different sizes. Say there are 3 buttons labeled, "A", "B", "ReallyLong". I still want them centered, with "B" in the middle of the screen. Equal spacers leaves equal spacing between them but not evenly distributed buttons. ReallyLong takes up too much room and B is not centered
You can create a constraint to align the centerX of a button to its container. Then, edit that constraint so that the centerX of the button is equal to the container's trailing attribute, with a multiplier of 0.25, 0.5, or 0.75 (and a 0 constant). To do this most naturally, you may need to tell Xcode to swap the first and second items, so that Button.CenterX equals Superview.Trailing (with multiplier) rather than the other way around.
Apple has explained it really well.
Creating Equal Spacing Between Views
To lay out several views that are proportionally spaced based on the orientation of a device, create spacer views between the visible views. Set the constraints of these spacer views correctly to ensure that the visible views are able to stay spaced apart based on the orientation of the device.
The following example uses the steps in the above task to show how to position two views proportionally spaced. The spacer views are annotated for the example, but are normally left empty with no background. First, create the two views and place them in the storyboard.
Add the three spacer views—one to the left of the leftmost view, one between the two views, and one to the right of the rightmost view. The spacer views don’t have to be the same size at this time because their size will be set through constraints.
Create the following constraints for the spacer views:
Constrain the width of spacer view 2 and spacer view 3 to be equal to
the width of spacer view 1.
Constrain the width of spacer view 1 to be greater than or equal to
the minimum desired width.
Create a Leading Space to Container constraint from spacer view 1 to
the container.
Create a Horizontal Spacing constraint from spacer view 1 to view 1.
Set this constraint to be a less-than-or-equal-to constraint with a
priority of 1000.
Create Horizontal Spacing constraints from spacer view 2 to view 1
and view 2. Set these constraints to be a less-than-or-equal-to
constraint with a priority of 999.
Create a Horizontal Spacing constraint from spacer view 3 to view 2.
Set this constraint to be a less-than-or-equal-to constraint with a
priority of 1000.
Create a Trailing Space to Container constraint from spacer view 3 to
the container.
These constraints create two visible views and three invisible views (spacer views). These spacer views automatically resize as the orientation of the device changes, keeping the visible views proportionally spaced, as shown in the following two figures:
I think I solved it. I have earlier been able to get even spaces BETWEEN buttons by inserting spacers between each button and setting the spacer widths to be equal. And addition is to also specify that the button widths are equal. This seems to be working well. All the text is centered in the appropriate place.
For 3 buttons, here's the one visual constraint that seems to do it.
Constraint = "H:|[spacer0(>=0)][button0][spacer1(==spacer0)][button1(==button0)][spacer2(==spacer0)][button2(==button0)][spacer3(==spacer0)]|"
With the above text it looks like this, which is what I was looking for. The middle button is centered, the right button is centered on the right third of the screen:

Resources