Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)];
}
like image 357
PPShein Avatar asked Dec 14 '25 15:12

PPShein


2 Answers

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];
    });
    
like image 192
Shrikant Tanwade Avatar answered Dec 16 '25 07:12

Shrikant Tanwade


// Get the supposed to be height of tableview 
CGFloat height = self.tableViewMenu.contentSize.height;
 // get the height constraint of tableview and give it you respected height
self.constraintHeightTableView.constant = height;
    // set the scroll 

NOTE:- don't forget to disenable the scrolling of table from storyboard or programmatically

self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width, self.viewUserLogin.frame.size.height + height);
    [self.scrollView layoutIfNeeded];
like image 38
Vaibhav Gaikwad Avatar answered Dec 16 '25 08:12

Vaibhav Gaikwad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!