How to edit multiple constraints at once in Xcode's Interface Builder - ios

I have a layout of UIButtons that looks like this:
The horizontal spacing between the buttons is 8. I would like to change this to 4. Of course, I could change them one by one, like this:
But there are a lot of buttons and I don't really feel like doing that.
I tried selecting multiple buttons and then added a horizontal space constraint of 4 but that just adds constraints. It doesn't update the old constraints. This creates conflicting constraints with the old ones.
I didn't see anything to solve this in the Editing Auto Layout Constraints documentation.

Just use Command + click to select multi constraits,
like the gif

One of the way is to create spacer views, With each view width equal to the first one. Set your Button constraint to leading and traling to the spacer view.
and if you change first width of spacer view it will change all othes view width.

I am agree with the answer of #Leo and up voted as well :)
Just adding to it, if someone would like to make it more easier, he can use the filter option given below the constraints.
For example to find all the leading constraints one can search for " = leading" word or "trailing = " for trailing constraints or other similar search string.
You can see this from the GIF as well.

You have to do that one by one. There isn’t any way that you select all of them an edit their values.

You just have to select all button and move where you want them and then you have to just update the constraint.

Related

Swift 4 - Issue with constraints on iPhone

I am having some major issues with my constraints, all I did was select add missing constraints and it looks fine in my storyboard and on an iPad like so,
But when I run it on iPhone, it looks like this:
What do I need to add to fix this issue...this is what I got so far:
Don't relay only on Add missing constraints , you have to make your edits after applying them
I see some constraints with leading
949,344 and 283+
and this may exceed width of some iphone screens
Simply remove any leading and trailing , and centerX the items with width constraints
also using UIStackView is good for the lower textfields
The easiest way to solve your problem with the constrains in my opinion for this type of layout is to embed your views in to Stack View. With Stack View you can easy center this views in to Screen by adding some costrains to the Stack View. With Stack Views if you need some adjustment you can do it only with few mouse clicks.
Select all your views
Click embed in the Stack View
Delete constrians that you don't need
Adjust alignment, distribution and spacing
Add trailing, leading and heigh constrian
Now it should looks good.

Autolayout UITextfield

I have been trying to achieve following layout in autolayout
I want to acheive this
may be i am doing something wrong due to which i am getting following result.I get this
can anyone correct me ?
This kind of layout is easily accomplished by using UIStackView, Please use UIStackView either in storyboard or in code.
Please check Apple documentation on it
https://developer.apple.com/documentation/uikit/uistackview?changes=_6
If you are using storyboard for Textfield then simply give leading and trailing to Textfield. Dont give the fixed width to textfield, it will work for all devices.
Same logic if you are setting textfield programatically. Dont give fixed width
As said above, use your element inside a horizontal stackview. Unfortunately, if you’ve a iOS target iOS 9.0, you will have to use another trick. Your stackview will become a normal view which will contain your elements to align. After that, you can the element width to be equal to their superview with the multiplier use.
Per example if you have 3 elements inside your view but you want that the first one being larger than the two others by setting the multiplier with a certain ratio like 2:4 for the first one and 1:4 for the last ones :-)

How to add bottom constraint?

I am using Xcode 8.I add UIView in bottom.I see is perfect in storyboard.
But when i run in simulator its doesn't look perfect. I don't know what the problem is.
I'm not sure if I understood your question correctly, but in Xcode you add constraints by ctrl+click+drag from the view that needs a constraint to any other view (or the layout margins). This may look like this:
ctrl+click+drag
On the size inspector (on the left side) you should then see all your constraints, so for example something like this: example size inspector with four constraints defined
This is where you can also edit the properties of your constraints.
Look for several things that can go wrong
Your UIVIewController's auto resizing subview is turned off.
UINavigationController's translucent property goes wrong.
Your top view height is fixed & some of your constraint are breaking and bottom constraint is one of them.
Your constraint priority is low, so breaking the bottom constraint.
in the console look for following:
Probably at least one of the constraints in the following list is one you don't want.
this indicates, some of your constraint conflicting each other, fix it.
If you insist on your question title, how to add bottom constraint, then here it is
NB::Hold the right click while dragging.

Why this layout constraint can't satisfy?

I have two UIView with fix size (30, 30), I want the views horizontal center in superview. here is the layout description.
H:[view1(30)]-20-[view2(30)]
V:|-20-[view1(30)]
V:|-20-[view2(30)]
and add a constraint at horizontal
view1.leftMargin = view2.rightMargin
But these constraints not working as my expect.
the runtime warning say that unable to simultaneously not satisfy constraints. why?
I find a solution to center the views, which add two placeholder view in left side ,and right side, but I want to known why my first solution not working , how to known my constraints satisfy ? What did I miss?
view1.left=view2.right and [view1(30)]-20-[view2(30)] is confict.
[view1(30)]-20-[view2(30)] means
view1.right+20=view2.left
view1.height=30,view2.height=30
so apple cannot make constrainsts.
I have tried to solve your problem and come to know that, with VFL, the only possible way for centering view in super view is through spacer views.
As you have defined your view1 and view2 also define two spacer views.
Then just paste this:
V:|-20-[view1(30)]
V:|-20-[view2(30)]
H:|[spacer1][view1(30)]-20-[view2(30)][spacer1(==spacer2)]|
And don't need view1.left = view2.right. Please remove it.
Output attached:

Use Different Spacing Values in UIStackView

Is there a way to use different values for the spacing between views within a UIStackView? As I understand it, the spacing property is used for all views (or rather the room between them). I was hoping to just set the top space constraint to some value for each view but this seems to be ignored. Any suggestion? Thanks!
It looks like there is no build in solution for this. So we went with spacer views. We just added plain UIViews with our desired heights as elements of the UIStackView. It's not beautiful but it does the trick.

Resources