Is it possible to set minimal height for cell? I use dynamic:
tableView.estimatedRowHeight = 83.0
tableView.rowHeight = UITableViewAutomaticDimension
At the auto layout code of the custom cell (either Interface Builder or programmatically), add the appropriate constraints.
E.g. (Programmatically in custom cell)
UILabel * label = [UILabel new];
[self.contentView addSubview:label];
NSDictionary * views = NSDictionaryOfVariableBindings(label);
//Inset 5 px
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-5-[label]-5-|" options:0 metrics:nil views:views]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-5-[label]-5-|" options:0 metrics:nil views:views]];
// height >= 44
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.mainLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:44.0]];
Have you tried creating a constraint in your custom UITableViewCell
's view of height >= 60.0
?
Set a contentViews heightAnchor to your least required height .
Swift 4.2 version programatically
contentView.heightAnchor.constraint(greaterThanOrEqualToConstant: <Required least Height>).isActive = true
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return (UITableView.automaticDimension > minimumHeight) ? UITableView.automaticDimension : minimumHeight
}
Got it. Made it work as below.
Drag and drop a View on top of UITableViewCell and set constraints Leading, trailing, top and Bottom as 0. Set height constraint as >= ExpectedMinimumSize.
In heightForRowAtIndexPath Delegatemethod:
-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}
In ViewDidLoad:
self.tableView.estimatedRowHeight = 60; // required value.
There is a trick which is answered by @Hytek. For this you have to give the constraint for minimum height.
For example: If there is one UILabel
into your table cell and you want that UILabel
increase the height as per the dynamic content. And you have code it like below.
tableView.estimatedRowHeight = 83.0
tableView.rowHeight = UITableViewAutomaticDimension
It will increase your label height when content is bigger but it also will decrease when your content is small. So if you expect that label should have minimum height then you have to give a height constraint to your UILabel
in a way that height >= 30.0
to your label.
This way your UILabel
will not decrease the height less then 30.0
.