ppshein ppshein - 10 months ago 86
iOS Question

How to dynamically increase height of scrollview based on uitableview

Currently, I've developed project that UIScrollView, UITableView, another 3 more UIView inputs and UIButton at the last. In that page, UIScrollView height will be dynamically increased based on height of UITableView.

For UITableView there is no more scrolling. Its height will be increased as well based on how many rows are added based on JSON data loaded by Async as follow.

productHeight = 44;
productHeight *= _nsOrderItems.count;
productHeight = productHeight + 100;

if (isHeaderTap) {
self.productTableHeight.constant = 50;
} else {
self.productTableHeight.constant = productHeight;

//then the tableView must be redrawn
[self.productTable setNeedsDisplay];

My Problem is I want to increase height of UIScrollView based on height of UITableView.

- (void)viewDidLayoutSubviews {
[self.scrollView setContentSize:CGSizeMake(_scrollView.frame.size.width, _btnEdit.frame.origin.y + _btnEdit.frame.size.height)];

Answer Source

You can definitely do that,

  1. First make sure your constraint of cells subView must set to top to bottom in order to calculate the height required for the cell.

  2. Make sure your delegated are set as below

    -(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
      return 44;
     -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
  3. Set height constraint of your tableView and make outlet of that constraint.

  4. Add below method to your class where you want to resize your tableView dynamically.

    - (void)adjustHeightOfTableview
        CGFloat height = self.tableView.contentSize.height;
        //CGFloat maxHeight = self.tableView.superview.frame.size.height - self.tableView.frame.origin.y;
        Here you have to take care of two things, if there is only    tableView on the screen then you have to see is your tableView going below screen using maxHeight and your screen height,
     Or you can add your tableView inside scrollView so that your tableView can increase its height as much it requires based on the number of cell (with different height based on content) it has to display.
       // now set the height constraint accordingly
        self.constraintHeightTableView.constant = height;
       //If you want to increase tableView height with animation you can do that as below.
        [UIView animateWithDuration:0.5 animations:^{
        [self.view layoutIfNeeded];
  5. Call this method when you are ready with the dataSource for the table, and call the method as

    dispatch_async(dispatch_get_main_queue(), ^{
       [self.tableView reloadData];
       //In my case i had to call this method after some delay, because (i think) it will allow tableView to reload completely and then calculate the height required for itself. (This might be a workaround, but it worked for me)
       [self performSelector:@selector(adjustHeightOfTableview) withObject:nil afterDelay:0.3];