Auto-layout issue - ios

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.

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

Evenly distributing buttons in Xcode

I have 4 buttons which are supposed to be at the bottom of my screen, right next to each other -- 0 pixels apart. I want it to work for any screen size, or at least for iPhone 5, 6, and 6+. I'm trying to add constraints, but it doesn't do what I want it to do.
It's my first time working with Swift and XCode, please give me a hint.
Thanks in advance!
The best practice is to use UIStackView
Just drop it to storyboard
Add vertical spacing constraint to bottom layout guide
Add horizontal spacing constraints to leading and trailing of the superview
Add height constraint
Now add the buttons as subviews of the stack view
set the stack views properties: Horizontal, distribution and alignment as you want
Good luck
Options are:
Set up constraints correctly
Use a StackView
Use a TabBar format
Use a ToolBar
Or, complex-ish calculations in code.
Select all of the buttons in interface builder so they're all highlighted and bring up the "Add New Constraints" popover. Add the left, right, and bottom constraints, and check "Equal Widths".

How to Align four buttons at dead center in Xcode 7?

How do I align these four buttons to be at the center (horizontally and vertically) for the diff sizes of screens. I see different sizes and positions of the buttons when I run the project in the different size of the simulator. Is there a way to make the four buttons to be at center (and same size) for ALL devices?
I don't have any experience with constrains, it has been a while since I used Xcode. I read tutorials about constrains for hours, but I still don't understand how to use it.
Screenshot
Thank you and happy holidays :)
The easiest way to accomplish this is to use a UIStackView. Very simple to use, and it automatically manages the layout for the buttons.
Remove the existing constraints for your buttons, then select all four and choose Editor -> Embed in -> Stack View.
Add two constraints to center your stack view in the view, and you're done!
Yes you can use Stack View, but it is only supported from ios 9, so if you are trying to have constraints below ios 9, follow below steps -
Add a UIView as container for your buttons, and add following constraints to it. (We will not give fixed height constraints to this view,let it determine its height intrinsically i.e we will use buttons to determine the size of container as buttons have their intrinsic size.)
a. Align Center X to superview
b. Align Center Y to superView
c. Trailing and Leading space
Now add first button and add following constraints -
a. Center horizontally in center.
b. Top space from superview (as buttons and have its intrinsic size so we dont need to give height and width, it will calculate from its content)
Now for rest of three buttons add following constraints -
a. Center horizontally with first button
b. Add top and bottom space to all buttons with their respective top and button views (buttons). For last button i.e 4th add bottom space constraints with container view.
Note - Now that top button have top space constraints with container view and last button have bottom space constraints with container view and all remaining buttons are connected to each other with top and bottom space, this will determine the height of container view and your buttons will be always appear in center of screen for all size.
Below is the link for screenshot with all required constrints explained above.
Screenshot with required constraints

How to add Equal Spacing and equal width for button in iOS Auto layout

Am new to Auto layout constraints in Xcode. I have one Bottom View like UITabBar with 6 UIButtons. Without constraints I aligned those buttons with 5 space padding with each buttons and each buttons has 50 width.
Now,I am trying to make it by using Auto layout to support all Screen sizes.
In Storyboard constrains I set equal width for 6 buttons and I made 60
space between each buttons. I set first button 0 to leading space and
I set the last button 0 to tail constraints.
I want the buttons with equal width and flexible spaces between views in all device sizes.
Can anyone pleas help me? Looking for the help. Thanks in advance.
check this image and made your constraints like below...
RESULT:- preview in different sizes
With Stackview (For iOS 9.0 and above)
NOTE: If you have to make app for iOS 9 and later then UIStackView is another option for you
With iOS 9 and Xcode 7, you can now have the expected result with UIStackView and only 3 or 4 auto layout constraints.
The following step by step uses Storyboard.
Create 6 UIButtons and 5 UIViews. Align them horizontally. Give some color to your views.
Select all your views and buttons and click on the Stack button. You will have all your buttons and colored views aligned in a UIStackView.
Select your stack view, go to the Utilities panel and select the Attributes Inspector item. There, click on the Distribution button and select "Fill Equally".
For now, your stack view's height relies on your buttons intrinsic content size. If you're fine with it, you can go to step 5. However, if needed, you can give your stack view a height constraint. Select your stack view, click on the Pin button, check the Height button, add the required value and click on the Add 1 Constraint button.
As an alternative, If you want each element of your stack view to have its height matching its width, select the first button of your stack view, click on the Pin button, select Aspect Ratio and click on the Add 1 Constraint button.
You can check that your aspect ratio constraint is correct in your Document outline (left panel) and, if you need, you can change it with a different constraint in the Attribute inspector (right panel).
Now, it's time to give some external constraints to your stack view. Select your stack view. Click on the pin button, make sure the Constrain to margin button is not select and set leading, trailing and bottom constraints to zero. Make sure that the bottom constraint is related to your view controller's view. Then, change the Update Frames button to "All Frames in Container". You can now click on the Add 3 Constraints button.
Your stack view is now set.
Further remark:
If you don't need your colored views width to match your buttons width, you can build a stack view with only UIButtons and simply add spacing to your stack view in the Attribute inspector. However, you will have to find a way to add a background color behind your stack view. Apple states about it in the UIKit Framework Reference:
The UIStackView is a nonrendering subclass of UIView. It does not
provide any user interface of its own. Instead, it just manages the
position and size of its arranged views. As a result, some properties
(like backgroundColor) have no affect on the stack view.
I've build a Xcode project with 4 different stack views:
one with colored views and relying on the embedded UIButtons intrinsic content size for its height,
one with colored views and with a UIButton "equal width and height" constraint,
one with colored views and with its own height constraint,
one without colored views but with spacing and embedded inside a colored view.
You can find this project on this GitHub repo.
iOS 9 has a new UIKit class called UIStackView. It is very helpful in stacking up views horizontally or vertically, like the way you want. You should check out this tutorial: Raywenderlich: UIStackView Tutorial
Select all the buttons and in Xcode’s Pin menu
Create a horizontal constraint from the top left view to the top right view by selecting the red line to the selected view’s right side nearest neighbor
Create a horizontal constraint from the top right view to the top left view by selecting the red line to the selected view’s left side nearest neighbor
Create a vertical constraint from the top view by selecting the red line to the selected view’s top side nearest neighbor
Select checkbox near to Height, Equal Widths and click on "Add 22 Constraints"
Spacer views should no longer be necessary unless you need space between borders.
You can create a StackView with equal spacing which will dynamically adjust the space between your stack views to use up all available space without altering the size of your subviews.
Interface builder can be a bit misleading here. the value you put in for spacing is the minimum spacing required not what the actual spacing will be.
The StackView will handle your spacing, making the size of your buttons equal you can use regular constraints for.

Resize proportionnaly between iphone 4 and 5 screens with XCode

I am trying to use auto layout in x code to correctly resize my view to switch between iphone 4 and 5.
I have a similar problem as the one presented in the picture. Let's consider the UILabel at the bottom is fixed.
I want the top UILabel to be at the middle of the space between the navigation bar and the bottom label. I don't know how to add a constraint that says "top space and bottom space must be equal". As a result, I can only fix one of the two constraints and I cannot get the expected result.
Thanks
The way I do this in IB, is to embed the top label in a UIView. Give that view a standard vertical spacing constraint to the top layout guide, and another to the bottom label. Also give it a fixed width. Give the label centerX and centerY constraints to this view, and it should keep it centered in different screen sizes.
How about adding a container view that fills up the space between the 1st label and the navigation bar, and then center the 2nd label inside the container view.

Resources