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
Related
I am trying to have two labels stacked in a UITableViewCell. I have AutomaticDimension and estimatedRowHeight set on my tableView.
I want the cells to show up stacked, with their dynamic heights if they both have content.
If only one has content, I would like it to be centered in the cell, or rather have the cell collapse to only show that label.
I have tried every constraint combo I can think of, but usually the labels either do not expand, or I end up with filler space where the label should be.
If the label has no text, I am setting label.text = "".
I am calling sizeToFit() on the labels each time I set the text.
What is the correct constraint combo? Here is what I currently have to start with.
you have to make two labels with leading, trailing, top and bottom constrains and don't add height constrains
make the number of lines for labels = 0
check the title label and from size inspector menu click on the vertical content hugging priority to be 250 not 251 as shown in the screen
image
in the viewController connect your tableview and add these lines
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 44
This could be done using StackViews in iOS,
in the Storyboard Select your both Labels and embed them inside a UIStackView
one of the benefits if you declared a good constraints it should automatically handle the requested behavior.
I recommend this tutorial to get started with UIStackView here
Better solution is UIStackView.
If still you want to go with UILabel you can try one more thing:
Setting label.text = "" will decrease the height of label but the constraint for distance between the labels will not make the label in centre.
For that add a referencing outlet of distance constraint and set it :
distanceConstraint.constant = 0
programatically while setting the label.text = "".
This will set the label with data in centre.
Hope this helps.
I am creating custom UITableViewCell and adding constraints programatically. But my same text not adjusting with in label and that resulting in different height for random cell. I have looked in to various tutorial and visited answers on other stack questions but not able to solve my problem.
As per my understanding this is the issue of priority between left and right label. I have also added this line while setting constraint.
[righSideLabel setContentCompressionResistancePriority:950 forAxis:UILayoutConstraintAxisHorizontal];
You can set the rowHeight property to UITableViewAutomaticDimension and put the constraint on the Label of your Cell and set the estimatedRowHeight and don't use heightForRowAtIndexPath method.Remember try to put top,bottom,leading and trailing Constraints.
Please help me. How to make adaptive labels for iPhone 5 and iPhone 6?
Labels on the left side should change its width.
Label on the right side should be right-aligned.
You should use Self Sizing Cells. Place the following lines of code in the viewDidLoad: method of your View Controller:
tableView.estimatedRowHeight = 36.0
tableView.rowHeight = UITableViewAutomaticDimension
The estimatedRowHeight is your custom value.
Using Autolayout first fix Left label from left side by assigning Leading space to SuperView.Same way fix Right label from Trailing.
As the right label has fix width it will be fix from right and as per the content size of Left label will vary. and if content increases the width and 'no of lines' property is 1 then it will truncate.
if you want label Left's height increase and decrese according to content then you can go for estimatedRowHeight property of UITableView.
I've seen a lot of guides about self-sizing cells in iOS 7 and 8. Unfortunately, all of them shows very simple cases (I mean two UILabel's with same width and which placed like the first under the second and their constraints are very simple too). So I've a situation that seems to be not so much difficult but I can't resolve a problem.
I believe that it is very easy for the most of people and hope that somebody can help. The problem is that there is UITableViewCell with 3 child views inside.
Every view is UILabel. Labels placed in such order: two labels at the top of UITableViewCell with fixed size and one at the bottom with fixed width but with dynamic height. How should I setup my constraints properly to make my UITableViewCell be self-sizing?
The key to self-sizing is a vertical set of constraints that will determine the cell height. This isn't that complex, as you don't have two horizontal labels that vary in height.
Since headerLabel and dateLabel height don't vary, you only need to constrain the dynamicLabel to one of the two top labels.
In this example, we'll arbitrarily pick headerLabel to use in the vertical constraints. The spacing between the labels wasn't specified, so I'll assume it's 10. Adjust as necessary.
For your horizontal constraints, set leading and trailing space to the superview.
Your vertical constraints would simply look like "V:|-10-[headerLabel]-10-[dynamicLabel]-10-|" (and should
dynamicLabel will grow as tall as it needs, provided you set its numberOfLines to 0, and its height will end up determining the height of the cell.
Assuming the constraints are setup properly, and the storyboard cell height exactly matches the vertical height set by the constraints, you shouldn't see any storyboard warnings or errors, and should be good to go.
8.4 does address a number of issues, so you shouldn't require any specific (layoutSubViews/preferredMaxLayoutWidth/reloadData) code to work around earlier 8.x problems.
As an aside, a general tip is to pin your constraints to the superview margin, instead of the superView. This means your leading, trailing, top, and bottom constraints could be 2, instead of 10, since the margin is generally 8. This lets your white space adapt to different devices, which is a really nice touch.
I have a table view with fixed height cells. In the cell there are 3 labels; name, address and type. The name label can be 1 or 2 lines while other 2 labels are only 1 line. I want to come up with something as in the screenshot. When the name label is 2 lines, it decreases the padding on the top and bottom of the cell.
So, I set the constraints between labels to be equal to some constant number. And also used inequality constraints between the name label and the cell's contentView. It actually worked and produced the screen below. But it gives me ambiguous layout in autolayoutTrace for the all 3 labels. I guess my inequality constraints are causing the problem.
How can I achieve this screen properly?
If you are sure that height of all labels are fixed and will not change in future then it seems you are doing in correct way. For ambiguous layout you may have sent some constraint that are not required or conflicting, you can debug constraints and make necessary changes. Reference :Debug Constraints
put the two labels in a container. Then set left margin, right margin, and center Y alignment constraints on the container.