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?
To get dynamic cell heights working properly, you need to create a custom table view cell and set it up with the right Auto Layout constraints. In the project navigator, select the Views group and press Command-N to create a new file in this group.
Use contentSize. height property of UITableView . I think you want to set the whole tableview with content size and then set the scrollview size related content of UITableView and for this use bellow code... After add data or reloadData in UITableView just set bellow code..
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.
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)
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
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.
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....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With