I'm having difficulty in adding the constraints to these 2 labels. I want them to be multiline and fit accordingly in the tableview cell. So what constraints should I put?
1.for 'first label' add left and top constraints to Content view of cell with value = 0 ;
2.and also for 'Second label' add right and top constraints to Content view of cell with value = 0 ;
3.create a horizontal spacing between labels.
4.and use UITableViewAutomaticDimension at heightForRowAtIndexPath
Related
hello guys now I have UITableViewCell should have dynamic height , this cell have 3 labels , I have added them in to stack and I added all requirements
to let tableViewCell be dynamic height
1- The code used for automatic cell height
notifications.rowHeight = UITableView.automaticDimension
notifications.estimatedRowHeight = 150
2- I have set constraints leading and trailing and bottom and top space to the superView.
3- The number of lines for each label is equal zero
this is cell before run
enter link description here
this cell after run https://ibb.co/5YCMpfK
The constraints of your tableViewCell must be like:
Properties of stackView:
Axis: Vertical
Alignment: Fill
Distribution: Fill Proportionally
Spacing: 5 (as per your requirement)
Number of lines of all 3 labels = 0
Check if your stack view doesn't have height constraint.
Check if your labels do not have any height constraint.
Give your stackview top and bottom constraints a specific value and make them greater than equal to topContraint >= 5 bottomconstraint >=5.
Change the property of stack view to Fill proportionally and give spacing according to your needs.
I have a static UITable view with 4 cells: the first and last cells are custom, while the second and third cells use a built-in style ("Basic" and "Right Detail", respectively). I have the following constraints on the label in the first and last cells:
label.leading = superview.leading + 20
label.Center Y = superview.Center Y
I'm having trouble matching the constraints for my custom UITableViewCell labels to those of the default UITableViewCells (the ones using the built-in styles).
For certain devices (e.g iPhone XR) the X position of the labels match for all four cells (20):
But for other devices (e.g. iPhone Xs) the X position of the labels do not match (16 vs 20):
Note the X position of the first and last cell is greater (20) than that of the second and third cell (16).
At first I thought that the constraints for the default cells were being varied based on the size classes of the device, but both the iPhone XR and Xs have compact width (wC) and regular height (hR).
Any idea how I can match my custom cell's label constraints to those of the default UITableView cell's label?
textLabel and detailTextLabel are present even in a subclassed UITableViewCell. While using textLabel instead of a custom label would solve the left margin issue, there is still a problem with UISwitch on the right side.
However, you could align your layout with a leading constraint of textLabel and a trailing one of detailTextLabel.
if let leadingAnchor = cell?.textLabel?.leadingAnchor {
myLabel.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
}
if let trailingAnchor = cell?.detailTextLabel?.trailingAnchor {
mySwitch.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
}
I have a custom table cell. It has two labels (title and description), one below the other.
What I have right now is, title label top = topMargin. And description label top = title label bottom + 10.
But in some cases, there will be no description. In such cases, I want the title label to be centered vertically inside the cell. Is this possible? What constraints do I have to set?
A UIStackView makes it easy to do what you want.
Create a prototype cell
add two labels
embed them in a stack view
set the stack view's properties to:
Axis: Vertical
Alignment: Fill
Distribution: Fill Equally
Spacing: 10
constrain the stack view Top/Leading/Trailing/Bottom to 0 to the cell's content view's default margins (or set your own "padding")
set your fixed row height - with default labels + 10-point spacing + top and bottom margins, you'll probably want at least 67
connect the labels to IBOutlets
When you set the text of the labels in cellForRowAt indexPath:, set the description label's .isHidden property to true if it has a description, or to false if it doesn't.
The result (with background colors for clarity):
The result without background colors:
how to fit only the height size of label ...
i have tried this:
servicedes.sizeToFit()
but this will fit both height and width... while i just want to fit the height of it so the text will be at the first line of the label ... my label is a multi lines label ..
this is my label
and this is what i got:
the space by the blue arrow is what i don't want ...
constraints:
how to do this?
Layout constraints aside, you should be able to use .sizeToFit() in a way that it only resizes the label's height by setting the width just before:
label.frame.width = label.frame.width
label.sizeToFit()
This approach won't work when using the constraints you're using right now.
Do the following:
Remove 'Center X' Constraint of the label
Add Trailing constraint(eg. 8) to the label and set relation of Trailing Constraint to 'greater than/equal to'.
Adding Trailing constraint:
Setting Relation:
I've tried Label.width = width constraint and get this:
But I want to get something like this:
So, the size of the label width equals the size of the table cell minus avatar width and paddings. How can I achieve this?
To achieve required effect you can add following constraints:
Cell prototype:
Label constraints:
After applying all constraints select cell's label and press ⌥+⌘+= to update label's frame