AutoLayout issue with ULabel - ios

I have 2 labels attached at top with "zero" constraint. One label have font 12, other 15. I face the problem, that i have unnecessary space between text and cell border. Please take a look:
You can see that left space to top is less then second. How to fix that?

You may want to check if the second label has option "relative to margins" checked, just uncheck it.
If you just want the labels to be on the same y position, just delete the second label's top constraint and add a baseline constraint between two labels.

Evgeniy Kleban,
As per your question you have two labels and for both the labels you have attached the constraint as 0 :)
My suggestion assign top constraint as 0 to only one :) Lets assume you are okay with the placement of label y :) Then delete the top layout constraint on label A16 and instaed hold control and drag from label y to label A16 and when you leave a popup appears select center vertically :)
This will ensure both the labels base lines will be aligned properly :) Hence your problem is solved :)

Related

how to setup constraint for 2 labels and one image view in Tableview Cell using storyboard?

I am new to swift development. I am stuck at one point. I want to add 2 labels and one image view side by side like shown below in the image. I have tried many combination but unable to achieve the desired result. I even tried placing both labels in one container view and image view in another container view and set the constraints to it but no success. Below is the output which I want to achieve.
Any help will be highly appreciated.
Apply Constraints like below:
Label 1:
Label 2:
Imageview :
hope,it's help to you.
I think, a picture can represent it in a better way. Please follow below steps to apply the constraints to your view:
1) Apply trailing, width and height to your image view. Still absence of y-position will throw error. Will handle in next image.
2) Align the image view, vertically centre to superview.
3) Do the same as step 1 and step 2 for you time label as well. Just give the leading constraint rather than trailing.
4) Give the leading and trailing constraints to name label and align it vertically centre to superview. Because you have given the widths to time label and image view to it will cover rest of the available width with 3 px padding from front and back.
5) Now, you will see a warning for this view, It is because you have fixed the time label's width and what happen if text will be larger for this label?. You can handle this situation by 2 methods
*First, change the autoshrink property from Fixed Font Size to Minimum Font Scale with factor 0.5. But it will just handle the case and will not remove warning. *
Second, update width constraint of time label with >=. But it will again create error because updating constraint to >= will conflict with name label's automatic width detection. To solve this follow next image
6) Decrease Horizontal Content Hugging Property of name label.
You can use horizontal stack view. In that you can place your labels and image horizontally.

How to build a layout of single line and multi line labels in iOS autolayout?

I have an application in swift,and have encountered an autolayout issue. I've come up with the solution to it, but it doesn't seem to work as expected.
So, here's the issue:
I have two labels in a table view cell. The first one should only have one line, and the second one must have as many lines as needed and take up as much vertical space as needed. The first label should always be shown fully and, therefore, determine the position, the size, and the number of lines of the second label. Texts for both are set programmatically when drawing the cell, of course.
The solution seems to be pretty simple: Anchor the first label to the left, center it vertically, anchor the second label to the parent top, right, bottom and the right side of the first one. Set the number of lines to 0 for the second label. The editor shows no warnings or errors.
This is how it looks like:
In most cases it looks good, like on the picture below, but sometimes not (red arrows at the picture below):
The first label should have absolute priority and never be truncated, wrapped, or cut, but this happens. What am I doing wrong?
UPD: For the question to be more clear, the first label is so called 'margin label', it represents the margin and the indicator of the cell. This is done to imitate a tree structure. The second is, of course, the text itself. '>' is for the closed comment, 'v' is for the opened.
Select the first label (the >) and go into the measurements panel in Interface Builder.
Near the bottom it will have a value for "Content Compression Resistance". Currently it will be 251 or 751 (I can't remember which) and it will be equal to the other label.
Change this value to 1000. This will stop it from compressing and make the second label compress instead.
set height constraint for first label and set 1 for 'lines' attribute
you can set the trailing of the first label is >= to the leading of the second label
Increase the content compression resistance priority of the first label. If needed increase the horizontal content hugging priority of the second label.

Align text fields using dynamic constraints

I've been learning how to make iOS apps, and have come across a minor annoyance. I can't find an easy way to align my text inputs.
Looking at the below screenshot, you will see that I've managed to align my text fields, which is what I want.
However, I did this manually by changing the individual Leading Space between each Label and Text Field.
My question is, how can I dynamically align my text fields?
Anand has posted nice way, but I personally use equal widths and heights in this kind of situation. So let's make a brief of it:
1) First, put constraints onto the age label:
left leading c.
top leading c.
width c.
height c.
2) Put constraints onto the top right textfield:
right trailing c.
top leading c.
height c.
the constraint, that will be related to spacing between label and textfield, so CTRL drag from textfield to label a select horizontal spacing c.
3) Then, make a second row of you elements - place second label and second textfield approximately of the same width and height in storyboard (with IB you can fix it later using magic function Update frames).
4) Then, set this for your weight label:
left leading c.
top leading c.
then select both labels and check equal widths checkbox, and equal heights checkbox.
5) The same repeat width your second textfield. You don't have to make spacing constraint in the second step.
FINISH Now all four items are aligned.
If you want to change height of labels for example, just select the height constraint of it and your other labels will follow this change as well.
So final experience is here in this animation:
Need to fix width of every label, every textfield's X- coordinate distance should me same according to bottom textfield's make sure every label's width should be same then add constraints to textField.

iOS AutoLayout - Label overlaps nearby label

I have a problem with a label constraint.
My goal is to have 2 labels on the same Y coordinate of a tableViewCell. One of the labels is pinned on the left side and the other is pinned on the right side.
Like that:
[This is the first label] [Second]
The first label should have a dynamic width based on the text which it has to display, BUT it should end about 20 points to the second label.
I tried that with numerous constraints, but sometimes the first label seem to push the second label out of the view and sometimes the first label just overlaps the second label like in this example:
The first label has constraints for:
Top Space to Superview
Leading Space to Superview
Bottom Space to a third label
Trailing Space to THE second Label (<= 20)
What is the correct way to display the two labels correctly?
Thanks in advance, appreciate your help!
EDIT
Tried the solution with giving the second label a maxwidth. Now, it is randomly working or not working. I don't get this at all.
Like others have said, you need to set the trailing constraint from the left label to the right label as greater than or equal to, so that there is a gap of at least 20 between the two.
But, you also need to designate which label truncates first. By not doing this, iOS will take it upon itself to choose, which is probably why your current solution works only some of the time. This is where Content Compression Resistance Priority comes into play. Set the value lower for the label you want to truncate first.
You can do this on your storyboard under the size inspector.
I'm assuming you want your left label to truncate first. In that case, make sure the left label has a lower horizontal content compression resistance priority than the right label. Notice how above I just dropped it to 749, as the default is 750.
Check out this answer for a good explantation on content compression and content hugging.
if your second label on the right side has fixed width or max width :) here is what you can do :)
Set the distance between left label and right label to >= 20
set the width of the right label with some value using [yourvalue] or [>=your value]
SO overall here is how label's constraints should appear
|-(distance_to_left_Label)-[left-label]-(>=20)-[right_lable(your_value)]-(distance_to_right)-|
Hope my answer helped :)

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