UILabels vertical hugging priority - ios

I have UICollectionViewCell with 2 UILabels that both can have 1 to N lines of text. I would like to use autolayout for calculation of cell height. I have set constraints for labels as follows:
When I run my app, cells are not displayed properly. Layout inspector says that my labels have ambiguous height. I've read quite a lot of possible solutions and tutorials how to achieve self sizing cells, but without any luck.
Thanks for help.

Just set the last label bottom constraint's priority to 749. That's it !!

Related

Width of UITableView Cell not changed when constraints are applied xcode storyboard

I'm using a custom tableview cell and auto layout but I'm facing the following error, i tried applying different constraints so that the width is stretched but found no success. Could someone please help me on how to adjust the constraints so that the cell fills up the entire width of the screen?
I've tried adding trailing constraints and equal width constraints but didn't get any success
Edit 1:
I tried this based on the suggestions and got this
And in the preview it's showing me this
Edit 2:
The same error occurs in another layout too
Here it is
The constraints added are
And the preview of smaller screen size
All elements outside of tableview are getting scaled properly, the error is occurring for elements inside tableview, the error being that the width of the cell is not getting adjusted according to the screen size
Could you please help me with this?
Edit 3:
Here's the constraints and output with colored background for better understanding, I still found no solution and can't understand why it is not working, could some one please help
Constraints:
Output in preview window:
Edit 4:
I created a simple layout to make it easier to solve the problem
And the output
The main problem is that the width is not scaling, can't seem to understand why...
Edit 5:
When I did the same layout on iPhone SE then these were the results, I've used the same constraints as above in Edit 4
Constraints don't seem to work inside the table view cell. Could some one tell me how this issue could be resolved? Thanks
From your Edit5, I believe you are want something like this.
Edit5 SE
Edit5 XR
These are the screenshots from XR and SE.
1. Let your cell calculate the height automatically. You just need to apply constraints on the items inside the cell.
2. In the UITableViewCell, add a UIView(background color: Yellow) with constraints top,bottom, leading, trailing equal to 0. Give a fixed height to this view if you dont want it to be equal to default row height.
3. In this yellow UIView, add another UIView(background color: Green) with constraints leading=10, trailing =10. Mark this green UIView "center vertically in container" to it's parent i.e yellow UIView. Now, give green UIView equal height constraint to it's parent i.e yellow UIView. You can change it's multiplier according to your needs.
Constraints on Edit5:
Edit5 Constraints
Moreover, for your Edit1, according to my understanding, you are not applying Content Hugging priority to Time label. Content hugging priority comes into play when available content is more than the total size of the content. Here, available content = width of the screen and total size of the content = Title+Time.
So, you need to set Time's Content Hugging priority high and Title's Content Hugging priority low for Title label to expand on your screen. The one with higher priority gets squeezed i.e hugs tightly it's content.
I have attached the screenshot of the screens XR and SE.
Edit1 SE
Edit1 XR
Constraints on Edit1:
Edit1 Constr
First take a UIView on tableviewCell and give leading,trailing,Top,Bottom to 0. and then design your upper view. and don't give any leading to Time Label.
If added the views in this hierarchy for the cell, it works fine with all devices.
Please check the constraints added.

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.

UICollectionViewCell inside UITableView Cell and Auto Layout

I have anUICollectionView inside aUITableViewCell and I have some issues regarding to Auto layout.
That's the hierarchy of my views.
UITableViewCell
|
-------ContentView
|
-------MyView
|
|-----UICollectionViewCell
Until here, all views are pinned up in his superviews., e.g: trailing space=0, leading space=0, top space=0, bottom space 0.
The height of myUICollectionViewCell is dynamic, based on the number of itens, which I draw usingUICollectionViewLayout. So it has a height constraint which is >= 100.
After the Items are in place, I call one delegate to update that height constraint, so myUITableView is able to calculate the right height for theUITableViewCell.
Even though this are working fine, sometimes I get the auto layout error above.
How can I avoid this?
The cell's contentView has a fixed height (44). That's causing the occasional conflict between the storyboard, and Auto Layout's derived height.
Self-sizing is the best way to go. What you want to do is to get your "collection view" to determine its intrinsic size, so Auto Layout can automatically handle the variable cell height for you.
See the detailed walkthrough by smileyborg in his answer to Using Auto Layout in UITableView for dynamic cell layouts & variable row heights.
He uses labels but the principle is the same. The cell asks its contentView to lay itself out. The contentView asks the label to lay itself out. The label determines its height. Auto Layout determines the contentView's height, based on the label's constraints, and the cell determines its height, based on the contentView's height. The point is that the label doesn't have a height constraint, and your "collection View" would no longer need one either.
Once you adopt self-sizing, it eliminates a lot of code (and size constraints), as containers manage their size, and constraints simply handle spacing.

Setting constraints for dynamic cell

I'm new to using constraints in my iOS projects, and trouble setting the right constraints for my dynamic UITableViewCell. I've tried every combination I can think of but it either won't dynamically change height, or it gives me warnings about ambiguous layouts.
My first label1 is not supposed to change in height, but the other two are. My current constraints achieve the desired effect, but are giving me the warnings seen under.
The warnings go away if I constrain the height of the labels (obviously), but that doesn't solve my problem.
Any suggestions on how to solve this would be appreciated!
First of all you need to set
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44.0
in viewDidLoad of your UITableViewController
Then you need to set constraints from top to bottom of the cell
top space to superview
vertical spacing
bottom space to superview
height greater or equal
and of course set lines to 0 for every UILabel
Example project is available here https://github.com/MihaelIsaev/SwiftAutoResizableCells

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