Autolayout multi line label in a table cell with fix height - ios

I have a table view with fixed height cells. In the cell there are 3 labels; name, address and type. The name label can be 1 or 2 lines while other 2 labels are only 1 line. I want to come up with something as in the screenshot. When the name label is 2 lines, it decreases the padding on the top and bottom of the cell.
So, I set the constraints between labels to be equal to some constant number. And also used inequality constraints between the name label and the cell's contentView. It actually worked and produced the screen below. But it gives me ambiguous layout in autolayoutTrace for the all 3 labels. I guess my inequality constraints are causing the problem.
How can I achieve this screen properly?

If you are sure that height of all labels are fixed and will not change in future then it seems you are doing in correct way. For ambiguous layout you may have sent some constraint that are not required or conflicting, you can debug constraints and make necessary changes. Reference :Debug Constraints

put the two labels in a container. Then set left margin, right margin, and center Y alignment constraints on the container.

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.

Multilined Label broken with position constraint

In a page of my application, I would like to add a Label text on top of a bottom bar.
I have succeeded that by adding the following constraint:
BottomBarImage.top = Label.bottom + 24
The problem is that the label is planned to show 2-3 lines of text.
I have tested on simulator and the result is only one line is showed.
Important note: By removing the constraint, the lines in Label are shown correctly (but the position of the Label is wrong of course)
How can we fix the multiline issue ?
Thanks
The problem is that your constraints are insufficient.
A label that is not under auto layout just sits there, inert; at layout time, its size is the size you gave it in the storyboard.
But as soon as you add a constraint to it, the label comes under auto layout. That changes everything. A label under auto layout is self-sizing: it automatically adapts to the size of its content. It will automatically adapt to the necessary height, but only if its width is fixed (so that it knows when to wrap). You have not given any width constraint for this label, nor have you given it a preferredMaxLayoutWidth, so the auto layout engine doesn't know how to do that.
So, to fix this problem, add a width constraint to your label or give it a preferredMaxLayoutWidth. Or, if you know both dimensions, give it both a width constraint and a height constraint. (And make sure the numberOfLines has been changed to 0; the default is 1 which is a single line only.)

Equal height constraints issue iOS Interface builder

I have four UILabel in a Row. Label's title is changing dynamically based on some conditions. Title may be multiline or single line. So all the labels should maintain same height means height of the Max height label. I added Equal height constraints for all labels using storyboard, But issue is suppose one Label have multi line it simply neglects the height and shows only one line, if two labels have multi line all the labels maintain the multi line height and shows correctly. I tried different constraints combinations but result is same, Maintain height only if two of them have bigger heights.
remove Height and Equal Height Constrain.
Just add Constrain from Top,Bottom,Left Right except Last label.
in last label Add Left,Right and Height Constrain
and use number of line will zero to all Label :)
Got a new idea. After u set equal heights change the compress / hugging priority ( higher value means harder to change )

Autolayout - 2 Labels with dynamic length

I have a question about Autolayout - normally i dont have any problems using autolayout, if a cell label has a dynamic length - but it this case ill got 2 labels, 1 top label with a dynamic length, and a second one under the first label.
Ill tried to set to the top label a leading, trailing, a fixed width (its an ipad landscape app, so i want to have and exact with) - and to the second one a bottom, trailing, and set the leading to the first label. Like here:
But i am unable to bring that to work that:
Both labels should have an dynamic length - so update the cell height automatically. Width of both Labels is 700px, width an margin top, right and bottom is 8.
Ill forgot to say, but the left label should have a dynamic height too.
Any ideas? Thanks in advance!
Errors after using "Lukas" Tipp:
By default, both labels will have a same hugging and resistance priority, making one of them less than the other would fix the problem. Ps, I'd avoid using magic numbers like 8 in your auto layout, instead use standard value

Auto-layout making the same width for each element

I am using auto-layout for my screen but despite I set the same width, same hugging priority and compression resistance(for all the textfield). I am having many warnings to ask me to set them differently but it looks different from what I want.
What I want:
1st row: The X seems to be hidden a part from the edges
2nd row: The textfields to
be the same width and the dash can be shrink if space is running
out(but visible).
Warnings:
It seems you just have the constraints a little bit off. This is how they should look like:
Add a leading constraint from the first textField to the superview
Add horizontal constraints between the textFields (from the first one to the second and so on)
Add trailing constraint from the last one to the superview
Select all 3 textFields and add an equal width constraint to them
This should work, so you can achieve what you need. Let me know if you need more help

Resources