35
votes

I am using autolayout in Xcode 5.

I set the table view's height to Greater than or equal to 200px. I want that it has dynamic size. Because sometimes it will have many rows, sometimes it will have a few rows.

But the size is always 200px. And if the content is larger than that, I should scroll down to see the lower rows.

What should I do to give the tableview dynamic size?enter image description here

5
Could you provide the limits you want? For example from 200 points to 400 points.Juan Catalan
Actually I don't have limits. Because there can be no rows, or there can be 10 rows.Burak
You said you want a dynamic size, meaning that the size changes. If the table has 0 rows which height do you want? Can the table occupy all the screen or do you want to limit it to a certain size?Juan Catalan
When there are 0 rows, height should be 0. And if there are more, there is no limit. Height is (row_count * row_height) + (section_count*section_header_height)Burak

5 Answers

65
votes

This is tested with the latest version of Xcode.

1) In Xcode go to the storyboard and click on you table view to select it.

2) In the Utilities pane (see picture 1) make sure the constraint for the table view height is defined.

Table view heigth constraint

3) In the view controller that displays the table view create an outlet to this constraint:

In Swift 3

@IBOutlet weak var dynamicTVHeight: NSLayoutConstraint!

In Objective C

@property (strong, nonatomic) IBOutlet NSLayoutConstraint *dynamicTVHeight;

4) In the storyboard go back to the height constraint for the UITableView and verify that the icon in the right has changed the color from purple to blue (see picture 2)

Table view heigth constraint after outlet

4) Also put this code in the same view controller:

Swift

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    tableView.reloadData()
}


override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    let height = min(self.view.bounds.size.height, tableView.contentSize.height)
    dynamicTVHeight.constant = height
    self.view.layoutIfNeeded()
}

Objective C

- (void)viewWillAppear:(BOOL)animated
{
    // just add this line to the end of this method or create it if it does not exist
    [self.tableView reloadData]; 
}

-(void)viewDidLayoutSubviews
{
    CGFloat height = MIN(self.view.bounds.size.height, self.tableView.contentSize.height);
    self.dynamicTVHeight.constant = height;
    [self.view layoutIfNeeded];
}

This should solve your problem.

These are the links to two versions of the sample project that does what you want, one for Objective C and the other one for Swift 3. Download it and test it with Xcode. Both projects work with the latest version of Xcode, Xcode 8.3.2.

https://github.com/jcatalan007/TestTableviewAutolayout https://github.com/jcatalan007/TestTableviewAutolayoutSwift

4
votes

create your cell by xib or storyboard. give it's outlet's contents. now call it in CellForRowAtIndexPath. eg. if you want to set cell height according to Comment's label text.enter image description here

so set you commentsLbl.numberOfLine=0;

then in ViewDidLoad

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

and now

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

voila ............ you did it....

2
votes

It can be done programmatically. The concept (and code itself) is actually very simple:

In the updateConstraints method of myTableView's superview, add a constraint so that myTableView's height is equal to myTableView.contentSize.height.

Tested on Xcode 6 targeting iOS 7.

2
votes

You can Ctrl+Drag the height constraint of your UITableView into your view controller source code and give it a name. Then in updateViewConstraints of your ViewController you can set the constant of that constraint to tableView.contentSize.height

....

@IBOutlet weak var tableViewHeightConstraint: NSLayoutConstraint?

....

override func updateViewConstraints() {
    super.updateViewConstraints()
    tableViewHeightConstraint?.constant = tableView.contentSize.height
}
-1
votes

Or you can satisfy height constraint from storyboard and check: [x]Remove at build time

enter image description here

Then, in your controller, update tableview's height which contains all your cells stuff. (*I use PureLayout as framework over AutoLayout)

enter image description here