Auto-layout making the same width for each element - ios

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

Related

ios swift custom uitablecell how to put several items (labels, images)

I have custom cell as shown in image below :
I want to make an indentation from leading and trailing edges, like below
Is it possible to do it by using autolayout?
I tried adding +5 to leading constraint with decrease Compression Resistance Priority like but it does not work.
So you need padding of 5 pt on the left ? Yes, you can do it with auto-layout. You may remove the constraint you marked in the image, and instead add another leading constraint form the stack view to the cell content view.
Another option would be to add a dummy view (or even better, a UILayoutGuide) with width as 5 pt, right to the left of this day label.

How to use auto layout to resize views in a table view cell?

I have a cell in which I place four buttons and four labels. Each button gets assigned a picture with width 50 and height 50. Furthermore, all buttons have a corresponding label describing what they're intended for.
My objective is to have the buttons and labels resize to keep the buttons' and labels' aspect ration intact while the screen dimension changes on different devices. I have been playing with auto layout changing the hugging and compression to achieve this but haven't been successful yet. Any help would be much appreciated...
I think you should take a look at a UIStackView, because this seems exactly as a use case for stack. Just put each pair button/label in a stack, and then all four pairs into a horizontal stack, which you constraint to the cell itself. You should be able to handle all you need just by configuring the stack’s properties (axis, distribution, alignment, spacing).
Embed your button and label into a view. Set the width of this view equal widths to content view and change the multiplier value to 1:4. This will adjust the widths of the views according to superview. Also, set the top and bottom constraint to 0 for this view.
Provide center align y-axis constraint to button after setting the width and height constraint to 50. Set its top constraint to a value you deem fit.
Set labels's leading and trailing constraint to a value like 8. Choose center alignment for text. Also, provide top constraint to buttona nd bottom to its superview.
Copy the view and paste to create the three views and provide them equal widths constraint to the first view. Also, provide their leading, trailing, top and bottom constraints.
Here are a fast tutorial in how to achieve that:
1-
2- completion of the first Gif:
Note you can achieve the same output using a UIStackView

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

Equal width and equal spacing between buttons in autolayout

I'm trying to use autolayout constraints to automatically resize a few similarly-sized buttons in a view to give the following effect:
Before resizing
Desired effect after resizing
As you can tell, I want the buttons to be of the same size and I also want the spacing between each button to be a constant 20 points. It seems pretty simple at first, so I set the following constraints:
Buttons: space from left neighbour = 20 (inclusive of left-most and right-most buttons)
Buttons: space from right neighbour = 20 (inclusive of left-most and right-most buttons)
Buttons: same width
What actually happens after resizing
When in preview or when I test run the app in my iPhone/simulator, the button resizes and doesn't even follow the same width constraint I set for it. In fact, the view containing the views also resizes to fit the new button sizes. Anyone knows how to fix this problem purely in the interface builder?
Setting:
- equal widths of all buttons
- horizontal spacing between all buttons
- leading to superview for the first button and trailing to superview for the last button
should do the job. Unless you're having problems with the superview (e.g. ScrollView missing constraints)
In the interface builder you set the spacing constraints between buttons like you described above. Then you can command-select all of them and specify the "Equal Width" constraint to apply to the selected objects.
Finally I have oblivion how to solve this problem. I've test it works like charm.
add constraints to space items with 20 units margin
add constraint to ages
now tricky part
for each item add constraint equal widths to a parent
select all this new constraints and change its properties
set multiplier to value 1:5
set constant to -24 (6 separation between items and parent edge gives 120, this multiplied by multiplier value 1:5 gives 24)
update items frames
That's it! Picture below show how it works in interface builder:
Set simulated size to "freeform" and test different widths (I sett this to 330).
This problem is seems to be because of wrong content hugging priority and content compression Resistance priority. So you should set them as low content hugging and high compression resistance (all should have same value).
Because content hugging is the property that resist a view to grow and content compression Resistance priority is to resist a view to shrink. For more information regarding these you can found this Question.

Auto Layout align two labels next to eachother

I am trying to convert my app to use Auto Layout.
This is how it is supposed to look (before I used Auto Layout):
Now I am unsure how to achieve the following using Auto Layout:
The left text label (with 22:35 in it) must be in the horizontal
center of the KL1032 label when the right label is not present (with
-14 min in it).
If the right label actually is present, then the right edge of the
22:35 label should align with the horizontal center of the KL1032
label and the left edge of the -14 min label should also align with
the horizontal center of the KL1032 label, leaving a little space
between the labels just as in the screenshot.
Which constraints do I need to use for achieving this? Do I also use Content Hugging priority's?
I tried just centering the 22:35 label, which is fine if the right label is empty/not present. But when the right label is present, this of course does not work correctly.
Embed each label in a view and give all the views a minimum width constraint of 14. Then the layout adjusts as you describe when the right label is empty. So, you layout the embedding views:
You're going to have to add and remove the "(-14 min)" label rather than just hide it or set it to be empty.
Set the 22:35 label to be have its horizontal center aligned with that of the KL1032 label, but with a somewhat reduced priority (say 750). Also set a constraint so that the trailing edge is greater than or equal to the center of the KL1032 label minus whatever slight spacing you want. This one should be at priority 1000 (required).
When the "(-14 min)" label should be present, add it and set up constraints on it. Constrain its leading edge to be the trailing edge of the 22:35 label plus the spacing you desire. Also create a constraint to align its center with the center of the KL1032 label, but set its priority to be between required (1000) and the constraint centering the 22:35 label, for example 800. The layout system won't be able to center it because that would force the 22:35 label past its required constraint, but it will get it as close as possible.
When that label should not be present, just remove it from the hierarchy, which will also remove its constraints. The 22:35 label will move back to being centered (because it can and it "prefers" to).
If you prefer, you can do the layout in the NIB with both labels present. Make outlets to the "(-14 min)" label and also the constraints on it. Make them strong because you'll be removing them from the hierarchy temporarily but don't want them released. That way, your code can just remove and re-add them as appropriate, without having to express the constraints in code.
Edit: Oh, and you'll want a constraint setting the baseline of the "(-14 min)" label to be equal to the baseline of the 22:35 label. You'll have to add that each time in code or set it up in the NIB with a strong outlet and re-add it each time, just like the others.
Edit 2: Another approach occurred to me. You could leave the "(-14 min)" view in the hierarchy and all of the constraints in place all the time. When you don't want it to show, set the view to hidden and set the constant of the constraint between its center and the center of the KL1032 label to be a much larger value. Definitely large enough to allow the 22:35 label to take its preferred position of being centered, potentially large enough to be well off-screen.
Since you don't want the 22:35 label to follow it all the way over, the constraint establishing the spacing between those two labels should be made to be "greater than or equal" rather than "equal". This change would not be conditional on whether the "(-14 min)" label is showing. It's just how that constraint should always be.
When you do want the "(-14 min)" label to show, reset the constraint that tries to center it back to having constant equal to 0. Also, of course, unhide it.

Resources