Constrain button within a view so that they are all evenly spaced - ios

I'm struggling constraining a set of button (see below), so that they the vertical space between them is equal. I am trying to create a subview with buttons that mimics a keypad. I constrain the top center button to the horizontal center and top space to view. I constrain the bottom center button to the horizontal center and bottom space to view. How do I get those middle 2 buttons to be evenly spaced? On android I would accomplish this by using dp (density independent pixels), however, if i try to constrain these buttons by vertical spacing, it will be different on every display because the pixel densities are different.

I think you look for a UIStackView so , compose your purpose with a set of horizontal stackviews nested inside a vertical stackView , Note this suggest for some items , if you will have too much buttons you can try UIScrollview/UICollectionView , key property of stackView that will solve your issue
distribution:FillEqually

Related

UIStackView as flexbox with centered justified content

I've been trying to center some buttons in an horizontal UIStackView
But doesn't matter what I do, the only not broken layout that I can get is basically:
I added constraints to fix the button width programmatically, as the amount of buttons is variable, but can't find a way to fix the spacing between them.
These are the stack view settings:
I tried all the distribution options!
The layout in the top image can be achieved as follows:
Add a stack view with default properties (horizontal axis, fill distribution). Add optional spacing as you want.
Constrain the stack view vertically to its superview.
Either don't constrain it horizontally to its superview, or if you want, constrain it using inequality (left >= superview.left, right <= superview.right).
Center your stack view horizontally within its superview.
Add your subviews. Make sure they know how to size themselves (either based on their own contents, or with a fixed width).
The key is the constraints on the UIStackView in 3 & 4. You're horizontally pinning it to its superview, so it needs to figure out how to fill out all the space. Instead, center it horizontally, and allow the stack view only to take up as much space as it needs.

Storyboard Constraint Issues: Half the height of the screen

I have a 4 labels in a storyboard, one large one at the top, and 3 smaller ones, with the bottom one centered in the middle(horizontally and vertically) like so:
I have a few constraints set already, like locking the top label at the top of the screen, and centering the bottom. However, I do not know constraints I should use so that all four labels are equally spaced, AND the bottom label remains at the center. I have played around with some constraints such as the spacing between the labels, but when I try to view this using a different screen size, everything is messed up:
Any ideas?
The best approach is a UIStackView with axis-> vertical , distribution -> fillEqually , so hook it's top ,leading , trailing to main view , drag the 4 labels inside it , and ctrl drag from the bottom most label to main view and select center vertically && horizontally
You can made a trick here:
Set the top global label top constriants and center horizontally to super view.
Set the bottom label constraints center vertical and horizontally to super view.
Take three dummy view between label one-two, two-three, three-four.
Set constraints between these dummy view as equal height.
Then set top-bottom contraints between label and dummy view properly.
Set horizontal constraints of the label and dummy view. I have set as center horizontally.
Here I have attached the image:
For better understanding I have colored the dummy view as light-gray. You can set it as clear.

Align button with respect to background image in XCode

I have a background image and I need to put buttons in it. I have tried auto layout as well by fixing it's height and width (The button text was not part of the background, has been added manually), but it is not working for all screen sizes. I want two buttons equidistant from line that can be seen(like in iPhone 4 inch screenshot, line is part of the background). How can I go about doing that? Are there any other tools which might be helpful? I am using swift
Best way to do this is to add a hidden view which has a background color of clear. Add a constraint for this which centers it horizontally and a constraint which positions its top where you want it.
Make the width of this transparent view the space you wish between buttons or leave it as 1 wide if you wish and use offsets in the constraints described below instead. Make the height 1 so it takes up no space.
For the buttons align the top of the buttons with the bottom of the transparent view. This places the buttons vertically.
Finally pin the trailing edge of button 1 to the leading edge of the transparent view and pin the leading edge of button 2 to the trailing edge of the transparent view. Set the offset for the pins to be the distance you want the buttons from center if you gave your transparent view no width.
On all screen sizes, the buttons will now be relative to the center.
If you want to have them equally space out, you can add transparent views to the left and right of the buttons and pin them together so they act like spacers. So superview->spacer->button->spacer->button->spacer->superview.
Using the spacer approach, select the 3 transparent views and add a constraint for equal width. You buttons will now be spread apart equally.

Autolayout, retaining button spacing for larger screens

I have a setup like this in the interface builder:
(iphone5 size). I want the buttons to stay in that layout if the screen size increases i.e widens (but the buttons stay the same size). If I pin the top two buttons to their respective container edges when the screen is larger they will be too far apart from each other. I have the bottom button increasing its size fine. How can I do this? I tried putting a transparent UIView between each button and the container edge, but couldn't get it working this way. Could someone give me a pointer on how to do this please? thanks!
Assuming you already have the width, height and y-constraints in place, to create the correct x-constraints you can try something along the lines of this:
check the Horizontal Center in Container option
choose your button and double click the newly created constraint Align Center X to
change the Second Item from Center X to Trailing
enter a constant value to offset the position
Do this for both buttons. The constant value of the second button should be the negative of the first one AND its Second Item should be Leading.
Result
This method will result in equal spacing on all size classes.
The only drawback is that the spacing is no dynamic. for that purpose you would need some placeholder view in between.
I would say there are two approaches you could take:
Assumption is buttons have constraints for width and height.
1) Use a transparent view which you center horizontally within its containing view. Then pin the left buttons trailing edge to the transparent view and the right buttons leading edge to the center. You have a choice as to how to define their vertical position.
or
2) Add a transparent container view to your main view and then move your two buttons inside this view. Pin the two buttons apart the desired width and then pin the other button edges to the containing view at size 0pts. Finally center the new containing view horizontally in the view. You will need to define the height of this container view from the top or bottom.
The second is probably easier to execute within storyboard.

Can't align 5 items in a row using autolayout

I am struggling to align 5 items in a row using auto layout as shown in the storyboard.
I can align the 3 items highlighted below but the 2 inbetween just don't want to align.
I have tried pinning the items to the buttons next to each of the items and setting the size but they refuse to align properly. Have also tried setting the 2 misplaced buttons to the leading and trialing container as shown below but these still do not align properly (or even closely).
Am I doing something wrong?
The way to do this is with spacer views. You need to add a UIView between each of your views and give them each horizontal spacing constraints to the view on either side. You'll need to edit these constraints so they are between the spacer's leading or trailing edge, and the view's centerX (the constant value should be 0). Give the spacers equal widths (but no fixed width), a fixed height, and a clear background color. You views on the left and right sides should have constraints to their closest edge, but the center view shouldn't have a centerX constraint (it's not needed). This will cause the views to be equally spaced in all screen sizes, and in both orientations.
Your best bet might be to create another superview (UIView) within which your button items are located. Then you can align your buttons more fluidly within that superview that encapsulates only those buttons rather than just wrestling with the constraints within the card superview, which it seems you are doing. Just make the background of that UIView clear, place the buttons on it, and it will appear exactly the same and you'll have a better way to anchor your views. This could also come in handy when you're targeting the functionality of just those buttons so you can grab them from the subviews of the button bar superview.

Resources