27

Is it possible to set minimal height for cell? I use dynamic:

tableView.estimatedRowHeight = 83.0
tableView.rowHeight = UITableViewAutomaticDimension

But I need to set minimal height for cell when my news title label text is in one line.

Jack
  • 13,571
  • 6
  • 76
  • 98
aaisataev
  • 1,643
  • 3
  • 22
  • 38

6 Answers6

66

Have you tried creating a constraint in your custom UITableViewCell's view of height >= 60.0?

Hytek
  • 768
  • 7
  • 8
  • 2
    how exactly did you solve it? can you provide some instructions? – Hlung May 20 '16 at 06:43
  • 7
    Drag and drop a View on top of UITableViewCell and set constraints Leading, trailing, top and Bottom as 0. Set height constraint as >= ExpectedMinimumSize. – YSR fan Jan 04 '17 at 05:11
  • 1
    You can do it without an extra View. If you have a large image and a text, just set the image bottomConstraints to >= x – DoruChidean Jun 27 '18 at 08:59
10

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.

enter image description here

This way your UILabel will not decrease the height less then 30.0.

Bhavin_m
  • 2,746
  • 3
  • 30
  • 49
9

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.
YSR fan
  • 705
  • 6
  • 11
  • Also worked for me in swift 3: `func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return UITableViewAutomaticDimension; }` In ViewDidLoad: `self.tableView.estimatedRowHeight = 60` – MrAn3 May 24 '17 at 21:57
  • 1
    I can't get this to work. My cell has a label and a switch, both centered vertically with respect to the superview, no vertical margins. At runtime, the cell's height is reported as `44.6667` even though I specified an estimatred height of `60` (like in this answer). It works if I add top/bottom margins to the label or switch, but I would have to caluclate those to give about 60 and feels inelegant... – Nicolas Miari Feb 16 '18 at 02:00
4

Set a contentViews heightAnchor to your least required height .

Swift 4.2 version programatically

contentView.heightAnchor.constraint(greaterThanOrEqualToConstant: <Required least Height>).isActive = true

Abhishek B
  • 157
  • 8
1

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]];
JapCon
  • 418
  • 5
  • 10
1
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return (UITableView.automaticDimension > minimumHeight) ? UITableView.automaticDimension : minimumHeight
}
Guest
  • 19
  • 1
  • please consider describe what you did in order to solve OP's problem – Derviş Kayımbaşıoğlu Dec 18 '18 at 13:23
  • This approach worked for me somewhat but auto resizing becomes disabled in my case which kind of defeats the purpose. Leaving the row height as UITableView.automaticDimension and setting a height constraint on the view works as needed for me. – Derek Hierholzer Mar 23 '19 at 18:30