UILabel autolayout cut bottom pixels - ios

I constructing UITableCellView from xib. This xib uses autolayout and two UILabels connected to superview and have vertical constraint.
Unfortunately it is cutting pixel(s) at the bottom of UILabel:
What do I missing?
UPD. Xcode is giving me error: Set vertical compression resistance priority to 749. If I play with compression resistance priority I get first label cut.

Set the priority on the vertical spacing constraint between the two labels to be lower than the vertical spacing constraint between the labels and the top and bottom of the view. Alternatively, in your delegate, override
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
and
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
that will return a height that will accommodate the constraints and label heights. By default I believe you get 44 pixels, so you can get a taller cell by overriding these. Also, you can set the cell height in IB if you're using Storyboards or Nibs.

Related

Self-Sizing TableView Cells based on two subview's height

TableView Description
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 60.0
Cell Description
I have a cell with 2 Labels(Title, Description) inside a ContainerView and 1 ImageView as below. Cell height will vary based on Description Label’s content.
Contraints of all views
There are two cases that I should handle
ContainerView.height greater than (ImageView.height + ImageView.Top + ImageView.Bottom). Here cell's height will be based on ContainerView.height
ContainerView height less than (ImageView.height + ImageView.Top + ImageView.Bottom). Here I expect Cell should consider (ImageView.height + ImageView.Top + ImageView.Bottom) as the height and make ContainerView vertically centre to Cell.
Expected Result in both the cases
Problem
If I set constraints for 1st case then 2nd case is not working and vice versa (I’m aware that by removing ContrainerView.Top, Bottom and making it Vertically Centre to SuperView case 2 result can be achieved)
Is there a way to achieve expected result in both the cases by using same set of IB constraints and UITableViewAutomaticDimension?
Give fixed height and width to the image view . Otherwise let the tableViewCell know the top and bottom of the imageview. So that it can calculate the correct cell height
First make sure that you are using self-sizing cells:
https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithSelf-SizingTableViewCells.html
Make Top and Bottom constraints for both image view and the container view to the edges of the cell and make them >=.
Alternatively, you could try Horizontal Stack View and make rigid (highest priorities) constraints to each edge of the cell.
Use these delegates,
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100; // height of default cell
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}
Edit
Try this one.
Your view hierarchy should be like this
ImageView Constraints
Add a view and put both labels into it.
Label Container contraints
Label Title constraint
Label Description constraint
Edit Output

Image view in table cell has height constraint with priority 1000 but image is squashed

I've got constraints for a table view cell as follows (the majority are XCode's suggested constraints).
But when the code is executed the row height of the cell is about half of what it is in the canvas.
Initially I didn't have an aspect ratio constraint and the bottom half of the image was missing when the view controller runs, after adding an aspect ratio constraint instead the image is squashed.
I don't understand why this is happening. Why when the height of the image view constraint has priority 1000 is it being ignored?
Try to set the height of the cell in
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}
like that it's look like you are not set the heightForRowAtIndexPath so that it tooks the default hight of 44

Dynamic UITableViewCell content does not expand cell

I have a storyboard with several different types of prototype cells with UILabels containing dynamic data. In my storyboard, the cell looks like this:
The UILabel's Lines property is set to zero to allow multiple lines of text. It is pinned to the top, left, right of the content view and to the nearest neighbor on the bottom (the blue line). The blue line is pinned to the left and right of the content view and to the UILabel at the top, and the UITextView at the bottom.The UITextView is pinned to the content view on the bottom, left and right, and to the blue line at the top.
When I run the app I get the following:
So the UILabel is forcing everything else down, as it should be, but the cell's height does not change as I want it to and thus the text view is being clipped off by the cell's fixed height. It was my assumption that if everything were pinned at the top and bottom, then the content view would be forced to expand. What am I missing here? Thanks!
It seems you have set your constraints correctly, just make sure that delegates are as below:
-(CGFloat)tableView:(UITableView *)tableView
estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 44;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
(If your constraints are set correctly from top to bottom)And that's it, you dont have to do anything else, auto layout will do its work smartly.
In addition to Auto-Layout constraint
try cell.contentView.LayoutIfNeeded statement in cellForRowIndexPath just before returning cell and HeightForRowAtIndexPath method
If you're sure all your vertical constraints are set and correct top to bottom, try setting tableView.rowHeight = UITableViewAutomaticDimension explicitly.

Problems With Dynamically Resizing UITableViewCells

I'm attempting to dynamically resize a custom UITableViewCell to fit an attributed string's content. This is the result:
When I initialize my tableView I do the following:
self.chatsTableView.estimatedRowHeight = 72.0;
self.chatsTableView.rowHeight = UITableViewAutomaticDimension;
I also implemented the following method:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
As you can see it is resizing my height (the smaller allowed is 72). However, for some reason it's creating a weird effect. I'm using storyboard, and auto layout.
Move it to the - (void)tableView:(UITableView *)tableView
willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath method.
You have to set estimatedRowHeight to UITableViewAutomaticDimension as well.
It should work only with the two properties you set , the row height and the estimated row height.
first try to remove the delegate method.
If the table "Jumps" a little when you scroll (because of a bug) then also implement the two delegate methods, heightForRow and estimatedHeightForRow and return the same values as you set in the properties , estimatedRowHeight = 72 and rowHeight = UITableViewAutomaticDimension.
If it's still doesn't work then I would double check the constraints.
For iOS 8+
Set up cell constraints. Make sure that ALL subviews that affects cell height are vertical connected. In this case: top edge of top label is pinned to superview's top AND top labels bottom edge is pinned to bottom's label top edge AND bottom's label bottom edge is pinned to superview's bottom. Vertical constraints chain have to be closed. In your case it looks like a bottom edge of bottom label is not pinned to contentViews bottom edge.
When dequeuing cell for reuse always use
dequeueReusableCellWithIdentifier:forIndexPath: not a shorter version without indexpath specified.
Set self.tableView.rowHeight = UITableViewAutomaticDimension;
Set self.tableView.estimatedRowHeight = 50.0;. This step is optional but helps table view proper manage things like scroll indicators etc.
Will work :)

UITableViewCell Autolayout issue

Scenario:
I have a custom UITableViewCell, and i have added a UIImageView inside it. I can add width, height, top, bottom, trailing, leading, center vertically/horizontally constraints to it, but i want to add constraint for aspect ratio. Storyboard doesn't let me add constraint for aspect ratio with Content View of UITableViewCell, so to overcome it i added another view with constraints top, bottom, trailing, leading (constant = 0) and then made UIImageView a subview of it and added aspect ratio constraint. After that i mentioned height of the cell using
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 60; // for example
}
This works fine.
Question:
Is this the good approach or there is another way to address same Scenario?
P.S : I would have added screenshots but forum is not letting me

Resources