nintyapple
nintyapple

Reputation: 237

UITableViewCell minimum height with automatically resizing cells

I am using automatically resizing cells in my project. I want to keep the cells at a minimum height, but I cannot set a minimum height constraint for my cells in my Content View (as it is greyed out in the Add New Constraints view:

enter image description here

Is there anyway to add minimum height constraints for my cells?

Upvotes: 14

Views: 11761

Answers (7)

Dasoga
Dasoga

Reputation: 5695

I added this constraint to my cell but in code (swift)

contentView.heightAnchor.constraint(equalToConstant: 80).isActive = true

If your cell view needs more space:

contentView.heightAnchor.constraint(greaterThanOrEqualToConstant: 80).isActive = true

Upvotes: 6

Aace
Aace

Reputation: 1822

Here's code used in viewDidLoad. cell is the UITableViewCell. Notice the relatedBy parameter is >=. 44 is my minimum height...

[cell.contentView addConstraint: [NSLayoutConstraint constraintWithItem: cell.contentView attribute: NSLayoutAttributeHeight relatedBy: NSLayoutRelationGreaterThanOrEqual toItem: nil attribute: NSLayoutAttributeNotAnAttribute multiplier: 1 constant: 44]];

Upvotes: 3

Jegan
Jegan

Reputation: 595

You can makes your cells as dynamic height by returning the UITableViewAutomaticDimension in height calculation methods of tableview. But for this your cell design constraints should be like the below one

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}

enter image description here

Note: Todo Title is the static height. And description will be dynamic height. So description does not own the height constrain. Important thing is you need set a number of lines as 0 for description label

Upvotes: 2

Krunal
Krunal

Reputation: 79656

Follow these steps, to set automatic dimension for cell with some minimum height.

  • Assign and implement tableview dataSource and delegate
  • Assign UITableViewAutomaticDimension to rowHeight & estimatedRowHeight
  • Implement delegate/dataSource methods (i.e. heightForRowAt and return a value UITableViewAutomaticDimension to it)

-

Objective C:

// in ViewController.h
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

  @property IBOutlet UITableView * table;

@end

// in ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    self.table.dataSource = self;
    self.table.delegate = self;

    self.table.rowHeight = UITableViewAutomaticDimension;
    self.table.estimatedRowHeight = UITableViewAutomaticDimension;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return UITableViewAutomaticDimension;
}

Swift:

@IBOutlet weak var table: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    // Don't forget to set dataSource and delegate for table
    table.dataSource = self
    table.delegate = self

    // Set automatic dimensions for row height
    table.rowHeight = UITableViewAutomaticDimension
    table.estimatedRowHeight = UITableViewAutomaticDimension
}



// UITableViewAutomaticDimension calculates height of label contents/text
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

For label instance in UITableviewCell

  • Set number of lines = 0 (& line break mode = truncate tail)
  • Set all constraints (top, bottom, right left) with respect to its superview/ cell container.
  • Note: Set minimum height for label, if you want minimum vertical area covered by label, even if there is no data.

enter image description here

Upvotes: 2

Abdul Rehman
Abdul Rehman

Reputation: 2456

simple added height constraint with minimum height you want and then change it relation to "Greater then Equal to". look highlighted section of screen short. enter image description here

Upvotes: 2

Giorgio
Giorgio

Reputation: 2210

When UITableViewAutomaticDimension is enabled the system calls UITableViewCell's systemLayoutSizeFittingSize:withHorizontalFittingPriority:verticalFittingPriority: method to calculate the cell height, so you can subclass UITableViewCell and override this method to force a minimum height for the cell, in this way

class MinHeightTableViewCell: UITableViewCell {

    var minHeight: CGFloat?

    override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {
        let size = super.systemLayoutSizeFitting(targetSize, withHorizontalFittingPriority: horizontalFittingPriority, verticalFittingPriority: verticalFittingPriority)
        guard let minHeight = minHeight else { return size }
        return CGSize(width: size.width, height: max(size.height, minHeight))
    }
}

And then

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "minHeightCell", for: indexPath) as! MinHeightTableViewCell
    cell.minHeight = 62 // change with your desidered min height
    cell.textLabel?.text = "some text"
    return cell
}

Upvotes: 24

Pushp
Pushp

Reputation: 1060

set a height constraint as height >= 60.0 No need to do anything

Upvotes: 10

Related Questions