Reputation: 237
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:
Is there anyway to add minimum height
constraints for my cells?
Upvotes: 14
Views: 11761
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
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
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;
}
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
Reputation: 79656
Follow these steps, to set automatic dimension for cell with some minimum height.
UITableViewAutomaticDimension
to rowHeight & estimatedRowHeightheightForRowAt
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
Upvotes: 2
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.
Upvotes: 2
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