iOS autolayout two UIViews to have equal width - ios

I have two views, v1 and v2, that are horizontally aligned inside a UIView. I want the views to have equal width. So I select the pin menu in auto-layout and select the width constraint. And then I go to the "Resolve Auto Layout Issues" menu and click on "Update Frames", but doing that just make my views v1 and v2 disappear. Does anyone know how I might get my views to have equal width? For comparison, in android I would place the views in a horizontal linear layout and give them both a weight of 1.

One of the views needs to have a fixed width, or the views need to be pinned to the edges of the superview and to each other so that you have a chain of constraints running from the left to the right edge of the superview. Once you have one of these two conditions, you can select both views and choose "Equal Widths" in the pin menu.

Is that the only constraint you're setting? XCode probably needs more information to avoid an ambiguous layout. In addition to "equal widths" you probably also need to pin the edges to the superview, set a distance between the 2 views, and perhaps try setting a min width constraint so they don't disappear. http://www.raywenderlich.com/50317/beginning-auto-layout-tutorial-in-ios-7-part-1

I'm sure that you could also select the two views, and in the "Alignment Constraints" select both "Leading Edges" and "Trailing Edges" constraints and then manually set the width of one and the other should follow suit.

I think anyone who comes to this Question and thread of answers should read the following answer too!
It shows a simple way to do this.

Related

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.

How do I properly set my constraints for an adaptive layout?

I am extremely bad at creating layouts for multiple screen orientations. All along I've been using suggested constraints on Auto-Layout but sometimes they don't work as desired. I've been trying to learn how set up my layouts properly by reading and watching tutorials, but I still cannot understand how exactly constraints work!
What I am trying to do is to make the button widths equally like so in iPhone 7:
But on other devices such as iPhone 4S, the second button width shrinks, which is not what I wanted:
Can someone please explain to me how constraints work in layman terms? I do not understand things like:
Leading/Trailing Space
Constrain to margins
Equal Widths and Heights
Top, Bottom, Baseline etc. (What do they even mean?)
Here's how to make two equal-width buttons:
Create two buttons
Add a vertical position constraint to each button. For example, you could position them in the center of the parent view using the "Align->Vertically in Container" function (in the lower-lefthand corner of the interface builder window)
Select both buttons. Add an equal width constraint between them using "Add New Constraints->Equal Widths"
Constrain the left edge of the left button to the leading edge of the parent view
Constrain the right edge of the left button to the left edge of the right button. Add space between the buttons using the "Constant" property of the constraint you created.
Constrain the right edge of the right button to the right edge of the parent view
Hope that helps! 😀

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".

Swift - Why ScrollView not full screen?

I had inserted a ScrollView into UIViewController and dragged ScrollView to fill the space between the navigation bar and the RAM label below:
But when I run the app, the ScrollView does not fill the space:
Please help me! Thank you very much.
P/S: Sorry for my english is bad.
The scroll view is not covering up the whole thing because you are running the app on a much bigger phone. The simplest solution is to run the app on iPhone 5.
However, if you want to solve the problem on all sizes of iOS device, you need to add constraints.
Constraints are things that tells a view when and how much it should resize and where it should be positioned.
To add a constraint, just select the view you wish to add a constraint to and go to the bottom right corner. You will see 4 buttons:
The leftmost button is used to embedding views in stack views. This is a feature of iOS 9. If your deployment target is lower, just ignore it.
The second button to the left is for adding constraints related to alignment - where the edges of the views are, what its baseline is and where it is positioned in the X and Y axes:
The third button to the left is used to add constraint related to margins, width, height and how the width and height should change when it is asked to resize (keep the aspect ratio, for example):
The rightmost button is used to let Xcode decide what constraints you should add. And I think most of the times its choices are okay. Sometimes though, you still need to do some tweaking before it works.
"So... what constraints should I add?" you asked.
Well, I think I should teach you how to think when you want to add a constraint. This way, you can figure it out yourself in the future.
You should first let Xcode guess what constraints you want. Just click the rightmost button and click "Reset to Suggested Constraints". This can save a lot of work if Xcode can get it right. So remember to always do this first.
Then, run your app on various devices and see if the view's position, size, and alignment are as you expected. If it is not, you might have to add and/or remove some constraints.
For example, if you found that your view is always the same size on different devices, (that could be bad because it means that some content my go out of view on smaller devices) it's probably because Xcode added a width and/or height constraint to the view. You should delete that so that the view's width and/or height is not fixed.
You can find your view's constraints in the view hierarchy:
Just select the constraint and press delete.
Uncheck Adjust subview option and add
scrollview.view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
You need to add constraints for your scrollView. Set the leading and trailing constraints to 0. Pin the height of your scrollView and also don't forget to set the top layout constraint. You can either pin the height or add bottom layout constraint to your page control.
Constraints are very important and its even more important to set it correctly. Check the Apple Documentation - Working with constraints in IB
Uncheck constrain to margins and add 0 every one of the four limits of spacing to nearest neighbour.
My guess (from the little information we have) is that you are creating a constraint from your scrollview to the top of your view with a value equal to the height of the navigation bar. Set the value of this constraint to 0.
Just set the 4 constraints to 0 to the area you need and then uncheck the "Content Layout Guides" checkbox in the constraints tab here. It will automatically adjust to the area you have specified.
The checkbox to uncheck

Using Autolayout to set scaling rules for objects

As you can see from the grab, I have all my spacing sorted and am pinning the leading & trailing edges properly; I am stuck on getting my buttons to scale horizontally so the space in-between stays the same. The conflict is all related to the fact that I haven't set a fixed width for the button, but in this case I don't want it fixed.
How can I resolve this autolayout conflict? Does it need some kind of width constraint? That defines a minimum at least?
select both buttons
click add constraints menu
select equal widths
select buttons one by one and add other constraints till all lines turn to blue like image below.
Hope it helps.

Resources