iOS - Swift 4 - Constraints for text fields side by side - ios

I Have a view like so:
And I am trying to add constraints so the text fields are side by side so the auto looks the same no matter the device width, how would I do that?
I have tried the following for all 6 of them:
But it ends up looking like this:
What am I doing wrong?

Ctrl drag from the description TextField to vendor TextField and select Equal Widths from popup
You can also use a vertical stackView where each row is a horizontal 1 with distribution set to Equal spacing and drag the text fields to it that will be more easier

Well, let's look at what we know about the horizontal axis (I assume you've got the vertical axis taken care of). There are two text fields; call them Left and Right. We know these things:
Where Left's leading edge must be
Where Right's trailing edge must be
The distance between Left's trailing and edge and Right's leading edge
The relationship between Left's width and Right's width, i.e. they are equal
Ta-daa:
(A UIStackView would construct these same constraints for you, but in a simple situation like this it's probably more trouble than it's worth.)

There are many ways to do it
I give you two but recommend the first one in your case.
First :
Define a constraint by draging one of your UITextFiled to the other one and set it to “Equal Widths”
You may also be able to define a percentage for each (60-40%) if you like.
Second :
Define a constraint like above (draging) And define a relative ratio (0.5) for UITextField width against its superview. Do it for the other one. You need to go to Object Inspector Tab and change the values if needed (so this requires two constraints, one for each UITextField as each of them are relative to the their superview) this is also a little bit slower than the first approach since margins should be defined with priority.

Related

NSLayoutContraints: How to position a bottom view below the higher of two labels?

I am working on an iOS 11+ app and would like to create a view like in this picture:
The two labels are positioned to work as columns of different height depending on the label content. The content of both labels is variable due to custom text entered by the user. Thus I cannot be sure which of the the two labels is higher at runtime and there is also no limit to the height.
How can I position the BottomView to have a margin 20px to the higher of the two columns / labels?
Both labels should only use the min. height necessary to show all their text. Thus giving both labels an equal height is no solution.
I tried to use vertical spacing greater than 20px to both labels but this leads (of course) to an Inequality Constraint Ambiguity.
Is it possible to solve this simple task with Autolayout only or do I have to check / set the sizes and margins manually in code?
You can add labels to stackView
One way to do this is to assign equal height constraint to both label. By doing this height of label will always be equal to large label (Label with more content).
You can do this by selecting both labels and adding equal height constraint as mentioned in screen short.
Result would be some think like below
The answer given as https://stackoverflow.com/a/57571805/341994 does work, but it is not very educational. "Use a stack view." Yes, but what is a stack view? It is a view that makes constraints for you. It is not magic. What the stack view does, you can do. An ordinary view surrounding the two labels and sizing itself to the longer of them would do fine. The stack view, as a stack view, is not doing anything special here. You can build the same thing just using constraints yourself.
(Actually, the surrounding view is not really needed either, but it probably makes things a bit more encapsulated, so let's go with that.)
Here's a version of your project running on my machine:
And with the other label made longer:
So how is that done? It's just ordinary constraints. Here's the storyboard:
Both labels have a greater-than-or-equal constraint (which happens to have a constant of 20) from their bottom to the bottom of the superview. All their other constraints are obvious and I won't describe them here.
Okay, but that is not quite enough. There remains an ambiguity, and Xcode will tell you so. Inequalities need to be resolved somehow. We need something on the far side of the inequality to aim at. The solution is one more constraint: the superview itself has a height constraint of 1 with a priority of 749. That is a low enough priority to permit the compression resistance of the labels to operate.
So the labels get their full height, and the superview tries to collapse as short as possible to 1, but it is prevented by the two inequalities: its bottom must be more than 20 points below the bottom of both labels. And so we get the desired result: the bottom of the superview ends up exactly 20 points below the bottom of the longest label.

Swift Center View Horizontal Ratio

I am creating a view within IB and and attempting to have 3 UILabels evenly space horizontally across the view. I came across this on SO, https://stackoverflow.com/a/30249550/4597666. I have three UILabels, each have the height and width constrained. Here is what the IB looks like:
I constrained each centered horizontally, and the first UILabel I have the multiplier 3:1, second, 3:3, third 3:5 like the post states.
When I run on my emulator, I don't get the result that I was expecting. It appears all three UILabels are centered horizontally, and the first and third are not offset.
Is there another setting that I'm missing, or another way to properly space the views evenly?
you need to make only one change.
Constraint you set is 3.CenterX to Superview.CenterX all you need to do is interchange the value so that you constraint should look like in below image.
Alternative solution. If you want to set constraints currently you have set then change the ratio from "3:5" to "5:3" and similar for all the labels.
Result:
Hope it helps you solving your problem.

Android weight in iOS ( equal width for 7 buttons)

I am trying to emulate the attribute in Android android:layout_weight="1" in iOS.
I have a View with 7 Buttons and I want that they all have the same width and the same margin between them. In Android you will put weight 1 for all of them and marginLeft = 5 (except the last one having margin right).
How I can do this on iOS with the interface builder?
You would use auto layout for this. For the leftmost object, set the leading edge to superview. For the rest of the objects, connect the trailing edge of one to the leading edge of the next. On the rightmost object, set the trailing edge to superview.
Check out this tutorial for the many many details.
http://www.raywenderlich.com/83129/beginning-auto-layout-tutorial-swift-part-1
This answer worked for me: https://stackoverflow.com/a/26323745/1463931
The key is to nest your views in a StackView,
now while holding down the Shift key, select the views you want to assign weights to,
then under the Constraints settings, select Equal Heights, or Equal Width depending our your desired outcome, save it.
Now open up the this Equal Heights constraints from the list on the top left corner of your StoryBoard (Where all your views and constraints are listed).
from the right section, edit the Multiplier value. This is where you enter the ratio of how you want your views to be distributed. Good luck.

Setting a constraint with dynamic trailing

I am trying to set a trailing constraint that changes based on screen size. Here's the example. Do I have to set a certain priority with >=?
What I want is for the plus button's trailing space to always be the same distance no matter the screen size. How do I set this constraint?
Okay I'll try to answer but I might be wrong since I might miss some information. For me there are two important components, the right most button (with a white cross in the middle) and all the other objects in the left. I assume other objects are somehow grouped and can be treated as a single unit. What you need to do is add new constraints from the storyboard. For the button, you at least need to add "horizontal space to the right" constraint and "width" constraint. For the left hand side, you need to add "horizontal space to the left" and "horizontal space to the right", but don't fix the width. I think this can achieve the visual effect you want. If it still doesn't work you might want to check this tutorial, http://www.raywenderlich.com/83276/beginning-adaptive-layout-tutorial, as your question is very fundamental.

Same width for a set of labels using auto layout

I need to create a simple layout with a few labels and buttons. Here's how I have positioned the UI elements in the IB.
This is how I want them to look like in the app as well. The label set on the left side with the same width so the elements on the right side will adjust their widths accordingly.
I have set the leading space to superview constraints on the labels on the left side and set vertical spacing between each of them. Similarly I've added trailing space to superview constraint to the labels and buttons on the right side with vertical spacing between each other. I've also added horizontal spacing between each couple of elements (ex: Name <-> John Doe, Telephone <-> button).
When I run it, it looks like this.
The elements only take up widths as their content needs. I assume this problem occurs because there is no way to auto layout to decide on a width. If I set the labels on the left to same width constraint, it doesn't work at runtime because the constants for them are fixed so they looks like this.
How can I make them appear properly like I have them in the interface builder?
Thank you.
I uploaded a test Xcode project with this issue here.
In your final example, it looks like you almost achieved what you wanted. You made all the labels on the left have the same width. You can now set a width constraint on the first label to a width that works for you, and the other labels should automatically match. The only issue you might still have is getting it to look good on wider screens.

Resources