Multiple buttons at center of screen vertically with Autolayout in iOS - ios

I want to add 3 buttons with same height and same width at the center of screen vertically. I am using auto layout.
I tried by selecting all buttons and set the constraint to align center vertically, but it's not working.

1 select the Button2 give hive constraints like first image.see the constraints window.
2 give the constraints to butto1.
3 give the constraints to butto3.
you can see all there button constraints in window.

try this constraint:
Step: 1>
Step: 2>
Step: 3>

add your buttons in UIView and set top,leading,trailing and fixed height constraints to every button. set constraint for views like center Y (vertically in container), center X (horizontally in container), fixed width,fixed height. make sure that your view's height is not more than it fits all buttons with padding between them.

Related

Constraint for StackView in Xcode so the buttons stay always to the center of the view vertically?

So i created a simple UIView. I added two UIButtons. I have added them in a StackView. I have put the Alignment of the stackview to fill in xcode attributes inspector and the distribution to fill equally and the spacing to 50. Then i added a height constraint of 300 to the stackview and i added a custom font as well to the buttons as well as three constraints. One for the stackview bottom as you see at the screenshot, one for the trailing and one for the leading constraint. I haven't put a top constraint yet. When i change to different devices through xcode the buttons are not centered. I added the top constraint but i guess because of the height constraint at the stackview there is an issue and all constraint become red when i change to another device than the initial i created the stackview. If i move for example to Iphone SE the buttons move to the top side i guess because of the bottom constraint... So is there a way to center the stackview with the buttons as i change to different devices? Any help appreciated.
For a stack view to be centered you needs leading, trailing and vertical centering constraints, stack view will have automatic height based on buttons heights and vertical spacing, no need for bottom constraint

Auto-layout issue

I have a view with two button. I have added auto-layout. But bottom space is more in iPhone 8 plus than iPhone 4s.I want to keep the spacing ratio of top and bottom same in all iPhone. How to fix it ?
Updated
For Specific Ratio : To my understood, We can give Multiplier for Centre Vertically Constraints. Here ratio of top and bottom space will be same in all iPhone series. I gave Multiplier as 1.4
==================================
I know having several answers there. But, no one used UIStackView to add two simple buttons.
Drag two UIButton, select that two UIButtons, click Embed in Stack icon from here.
Stackview Properties
Give following things for UIStackView,
Set Axis as Horizontal, Distribution as Fill Equally, Spacing as 10 in Attributes Inspector
Stackview Constraints 1
Give constraints for that UIStackView, Check/Uncheck Margins, Left and Right = 0, Height as 30.
Stackview Constraints 2
Still, some constraints are not satisfied. Press Control and hold it, Click StackView and Drag to SuperView, you will get black pop up. Choose Center Vertically in container.
Stackview Constraints Customization
If you want to change height of UIButton or Bottom Space, click UIStackView and in Size Inspector, you can change height or move up/down.
If you are using Auto layout then set constraint like this way
set constraints to bottom and leave top constraints
1) Select both buttons
2) Click on "Resolve auto-layout issues" button on bottom and click on "Reset to selected Constraints"
You should have 2 stack views. One Horizontal that contains the 2 buttons, and another vertical that contains the previous stack view. Set the horizontal stack view to "fill" and "equal spacing" and the vertical stack view to "fill" and "equal centering." Below I've included a couple of pictures to illustrate what I mean.
THE ONLY CONSTRAINTS ARE ON THE STACK VIEWS THAT BIND THEM TO THE SIZE OF THE CONTAINER VIEW, OR PARENT STACK VIEW.
This allows you to truly create an auto layout that scales with the device's screen size.
Use Constraints like this. These buttons have bottom fix with superview.

AutoLayout in iOS using storyboard only

I have a ImageView whose top constraint is 80 and center. I have two textView with margin of 100 from top consecutive object and 100 margin from both left and right.
When I am trying to implement auto-layout i am not able to do so because of many warning as shown in the figure :-
your imageview and textviews are not comfortable with your current constraints .if you add top and centre constraints for uiimageview you must add the height and width constraints for accessing the x value.
and also you just add height constraints for both of your textviews and try again. thankyou
Every time when you give constraint do following things :-
Fix the width and height of the element.
Give one vertical constraint and one horizontal constraint.
Don't do over-constraint.
Update it where it is showing warning.
If you set Left/Right margin and CenterX, your system is over-constraint.
You should set : Left + Center or Right+Center or Left+Right.
Next you should change the content Hugging and Compression priority.

How to Align Two UI Button Horizontally Aligned in A Xib Using Size Class and Auto Layout

How to Align Two UI Button Horizontally Aligned in A Xib Using Size Class and Auto Layout. Two Button should keep its original height and width
i would like to achieve this without code
Thanks in advance
First add gallery button:
Height constaint
Width constraint
Leading space with superview
Top space with superview
Then add camera button:
Horizontal space with Gallery button
Equal width with Gallery button
Equal height with Gallery button
CenterY with Gallery button
And it should be done.
Check this video, to set the constraints
You can check in the preview,
Select both Buttons.
Add the following constraints show in image
This will add default height depend on Text Font.
So if you want to increase height then Select any one button and add extra height constraint. Update the frame.
Just simple select Two Button and Give horizontal and vertical spacing constraints and give Equal Width and Equal Height to Button.
Connect your Gallery button to your main view's corner and set it using
Next, connect your Camera button to Gallery button and set Horizontal Spacing.
Inside your Storyboard's Leading Alignment Contstraint, set Gallery.Trailing as First Item, and Camera.Leading as Second Item. Set Relation to Equal.

Xcode UIBuilder - placing a button X pixels above the vertical center

I have several buttons and i want the lower one to placed in center Y plus 10-15 pixels.
I am trying to do this with the auto layout constraints, but i can't figure out what should be the correct constraint.
Add the Vertical Center in Container constraint (Alignment constraint button on the interface builder) to the buttons and change the constant to -10 to -15 as appropriate. You will probably need a x constraint, height constraint, and a width constraint as well.
I don't think it's possible to place an item "N points above the center." Instead, you can create a plain UIView with a transparent background and center that in your window. Give it a height of 10 points or so. Then you can use auto layout to place your buttons ~5-10 points above that.

Resources