UITableViewCell Constraints - ios

So I've been trying to make a custom layout of a UITableViewCell that it contains an image and label, as you can see it in the picture below.
The main problem is the compression and hugging I haven't put my finger on it and I can't understand why the image expands, I have changed the compression and hugging of both
IMAGE SETTINGS:
LABEL SETTINGS:
Any Suggestions on how I can fix the shrinking of the label?

Content hugging is scaled so that larger numbers mean "do more hugging". In other words, your label (Content hugging priority = 251) will try to make its size match the content size more than your image (Content hugging priority = 249).
To do what you want, you need the priorities the other way around.
That said, you'll probably find that the layout is better anyway if you set the image to be a fixed width or fixed aspect ratio, otherwise it's going to look weird when these are in a table. If you do that, then you won't need to adjust the hugging priority because everything will be constrained.
Also, #matthews is right, you should get rid of the Center Y constraint because it's meaningless when it's together with the Top and Bottom constraints.

You can give constraints for ImageView i.e Leading,Top,Height(Itself),Width(Itself) and Trailing space to label,then for label i.e Top,Trailing and Height itself.Hope It will work as per your requirement.

Hope this may help you:
For UIImageView
For UILabel
Use below code for self sizing cell
tableView.estimatedRowHeight = 100.0
tableView.rowHeight = UITableViewAutomaticDimension

Related

Strange behavior of custom UITableViewCell label

I don't know why, but my label behaves strangely. You can check screenshots bellow. Image should fill stackView.
Screenshots
Since you really ought to share a bit more, I can only try to answer based on what's available.
The problem is most likely due to the fact that the StackView that surrounds the label and image does not have a width or height constraint of any sort and/or the imageView has a very low content compression resistance property.
Because StackViews calculate their size based on their child views, it will try to find the best fit. If the label has a normal compression resistance but the imageView has its resistance set to low, the stackView will simply calculate its frame based on the label alone.
So to fix it check the compression resistance of those objects or define contraints for the surrounding stackView to match its parents width.
Firstly.. What is it that you are trying to achieve? Image and Text one beside the other, or one below the other? You might want to check for the stackview direction property
Secondly if you want that your image size remains constant, you will have to give your image view some fixed width constraint, and stackview will respect the size of the imageview. Or if you want that the image takes the entire width of the screen, you can basically pin the stackview to the edges of the screen, after you have put in the imageview inside of the stackview.
Hope that makes sense.
Comment back if you need more help :)

can not set layout correctly when adding several uilabels to uitabelviewcell

I came across a problem when configuing the cell of UITableView. I add two labels vertically in the content view of the UITableViewCell, and I also add constraints for the top, leading and bottom layout attributes:
I think that the height of the cell can be caculated dynamically as I have set all the vertical layout and with the instrinct size of the label, the height can be inferred.
so, I can not understand the error message that IB told me.
The second problem is that the height of cell appear on the IB is not changed with the constraint I`ve make. If I decrease the bottom constraint for example, and it is the label to change its size to fit the constraint, but not the cell change its height.
If you need to add top , leading and trailing(or width) to the 1st label. Then add bottom ,leading and trailing(or width)for the bottom label. Then add bottom constraint for 1st label to 2nd label.Then by selecting both labels, add equal height constraint.It will solve your problem.
The meaning of this conflict is that when your label content is increasing dynamically, which label's content is needed to give more priority before whom.
More precisely it can be said that if you increase one of the label's content hugging priority i.e. 252 then that label's content increment and size will be given more priority for incrementing it foremost. As autolayout executes according to the priority of constraints, it faces ambiguity in terms of increasing the views of labels if you do not set the content hugging priority.

UILabel is not resized with autolayout

I set inset constraints for 2 labels, and width is set to fixed value. number of lines is set to 0.
What I expect is the UILabels are resized to fit, and the parent view is also auto resized. But the output is the container remains the same size but the sub uilabels are either too high or too short. What's the problem? Thanks.
Here is the constraints
Try setting the content hugging priority to a higher value. This affects how resistant a view is to expanding past its intrinsic content size.
The image of UILabel looks like it is truncating the contents. So if you want to display the text in the next line if it is bigger then you need to use the below api.
label.numberOfLines=0;

Allow UILabel to grow dynamically using auto layout

I have a label which is going to contain a big description. I want the label to continue growing on new lines. In the image, its the label which starts with event_venue.....
The even_venue.. label has 3 constraints for now:
Vertical space with eventt_title
a horizantal space with the leading of the superview
a width constraints which defines that the label width is always less than the superview.width.
What I want to acheive is to make the event_venue.width less than superview.width, but if it has more text, it should display in new lines. Is this possible using autolayout?
This are possible steps which can create expandable UILabel
Set layouts for UILabel fixing its position
Set number of lines = 0
Set content vertical compression resistance to 1000 (this will allow text to push the label)
Since you want UILabel to expand you cannot give it fixed height constraint or its parent fixed height constraint. Sometimes depending upon condition giving height constraint is necessary to avoid error then you need to set its priority lower than vertical compression resistance
Yes, this totally is possible. I see answers here that are close to solution but not complete. Here is a solution which works with auto layout in Storyboard, no coding of sizeToFit or anything. Your modified steps would be:
Vertical space with eventt_title
A horizontal space with the leading of the superview
A horizontal space with the trailing of the superview
Set UILabel's Line Breaks as Word Wrap.
Set UILabel's lines property as 0.
I have solved a similar problem. I had to make a label that had a variable amount of text. Here's what I did:
In the storyboard, place your label with the origin where you want it.
In the Attributes Inspector, "Label" section, set the Line Breaks = Word Wrap
Fill the label with random placeholder text to the maximum shape you want. For example, if you wanted to fill the whole width and have room for a maximum of three lines of text, you could do:
abcdefghijklmnopqrstu
abcdefghijklmnopqrstu
abcdefghijklmnopqrstu
In the code, set the text of the label using setText:
[self.myLabel setText:#"MyLabelText"];
This did it for me. Your situation may be a little different in that I wasn't changing the width of the superview and it sounds like you might be. But if the width constraint is set on the label then I would expect this to work in your case, too.
I had a similar question about label resizing, and the answer that I found that was useful to me is here: UILabel Auto Size Label to Fit Text. This is a good starting source for code on how to resize your label programmatically.
I would recommend that you also add a horizontal trailing auto layout constraint from the label to the edge of the superview. Do that and you can then get rid of your current width constraint.
AutoLayout facilitate you for orientation purpose. I don think it will give you automatic expansion. You have to define label with width and height completely, otherwise you will see dots at the end of label. So you may use UITextView expanding it all over the screen. And set textView.backgroundcolot = clearColor.

Label's height issue in tableView with auto layout

I use autolayout. I am displaying 2 labels in custom UITableViewCell. Label1 is above Label2. Their text is dynamic.
The issue is the height of one of the labels when displayed is larger than its text.
I tried changing their Content Hugging Priority.
So what happens is, if that priority is same or Label1's priority is higher, then Label1 is having exact height to fit its text but Label2 has larger hight than required. And when Label2's hugging priority is higher than issue is with Label1's height.
Any idea how to solve it?
It looks like you are expecting your cells to auto-size based on auto-layout's constraint information, but UITableView sizes it's cells using frames/autosizing masks. If this is the expectation and you aren't autosizing the cells like in this question, then your labels are going to either force-clip themselves in order to fit inside the cells with the sizes they were given from the table view or grow to satisfy all of the margin-constraints.
Since the content hugging and content compression resistance priority values are less than required (less than 1000), they are considered "optional" and will be satisfied as close as they can be without violating any of the other required constraints. This is why your label begins growing (or clipping itself).
This can be solved in a couple of ways off the top of my head:
If you don't care about the cell having a variable height and are fine with the labels migrating toward the top of the cell, then make the constraint that pins the bottom label to the lower edge of the superview be non-required. More specifically, make that constraint have a priority lower than the vertical contentHuggingPriority for both of the labels. This way the content hugging priority constraints will take precedence over the lower constraint.
Make your cells auto-size themselves (using auto layout or otherwise) so that the system never has to consider the vertical contentHuggingPriority of each label 'optional'.
I solved this after experimenting with lot of things. The only thing I had to do is to set horizontal and vertical content compression resistance priority to required.i.e. 1000.
I did this for all labels because I don't want any of the labels to trim their content.
One more thing which is too much important is Getting Right Height Of Cell. If there is even 1pt of error in calculating custom cell's height it will not be displayed as expected.
Hint :
If height of any view is greater than expected then possibly calculated height of cell is greater than what is actually required.
If any of views is shrinking vertically or not displaying whole content then possibly calculated height of cell is lesser than what is actually required.
Yoy can test if height is wrong by adding/removing constant value to height (variable) you calculate for cell.

Resources