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

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.

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 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

UIScrollView with multiple multi-line labels and AutoLayout?

Is there a way to achieve this? I have tried literally everything and nothing has worked for me yet.
So basically what i want to do is the following: I have a scroll view with some labels in it. All the labels get their text from a server and I have set their number of lines to 0 so that they change their height according to the amount of text. However, this does not affect the scrollview content size(even though my labels have constraints set up to the bottom,top,leading and trailing of the scrollview) and the labels go off screen and I am unable to scroll down. Can someone point me in the right direction to how I would set up my constraints, my view hierarchy and etc?
Any help is much appreciated! :)
Late, but this solved it for me:
Set leading (I have a 32pt inset), trailing and top constraints. The trailing will not actually seemingly do anything..
Make the trailing Greater Than or Equal to avoid localization alert.
Finally, add a new Equal Width constraint to the label matching the scrollview. Use the constant to subtract the required padding (I used 64 due to mirror my leading inset).
And voilà! The Label will align correctly both in IB and in-app.
In Scrollview the last view's bottom constraint is so important. You should set its priority to 250 and put it to Greater than or equal.
Remember you should only change the bottom constraint of the last view, which in my case it's the continue button.
I would consider using UITableView instead, it has several benefits:
It allows for reuse of cells, if all the cells look the same
It manages recycling of cells when the number of values you're getting from the server increases (decreases memory pressure when number of cells becomes substantial)
It allows for more flexibility with the content (it's quite often for design to change last second or to evolve over the course of the project)
Most importantly, UITableView support auto sizing cells (as of iOS8), you need to specify the constraints between the label and the borders of the cell
There are several resources to start with:
http://www.raywenderlich.com/73602/dynamic-table-view-cell-height-auto-layout
https://www.captechconsulting.com/blogs/ios-8-tutorial-series-auto-sizing-table-cells
http://www.appcoda.com/self-sizing-cells/
Use a container view in a scrollView
Add constraints to superview (leading, trailing, top, bottom, height,width)
Make IBOutlet of constraints that you are going to update.
Add you all labels inside that view.
Update constrains/frame of your label so that it fits the text.
How much you increase the label height you should increase the container height too.
If the label count is not fixed use custom label class to add subview.
Perhaps you should need to understand how ScrollView works in Storyboard with autolayout.

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