How to position UITableViewCell at bottom of screen? How to position UITableViewCell at bottom of screen? ios ios

How to position UITableViewCell at bottom of screen?

I've create a new sample solution since the previous answer is not outdated for modern use.

The latest technique uses autolayout and self-sizing cells so the previous answer would no longer work. I reworked the solution to work with the modern features and created a sample project to put on GitHub.

Instead of counting up the height of each row, which is causes additional work, this code instead gets the frame for the last row so that the content inset from the top can be calculated. It leverages what the table view is already doing so no additional work is necessary.

This code also only sets the top inset in case the bottom inset is set for the keyboard or another overlay.

Please report any bugs or submit improvements on GitHub and I will update this sample.


- (void)updateContentInsetForTableView:(UITableView *)tableView animated:(BOOL)animated {    NSUInteger lastRow = [self tableView:tableView numberOfRowsInSection:0];    NSUInteger lastIndex = lastRow > 0 ? lastRow - 1 : 0;    NSIndexPath *lastIndexPath = [NSIndexPath indexPathForItem:lastIndex inSection:0];    CGRect lastCellFrame = [self.tableView rectForRowAtIndexPath:lastIndexPath];    // top inset = table view height - top position of last cell - last cell height    CGFloat topInset = MAX(CGRectGetHeight(self.tableView.frame) - lastCellFrame.origin.y - CGRectGetHeight(lastCellFrame), 0);    UIEdgeInsets contentInset = tableView.contentInset; = topInset;    UIViewAnimationOptions options = UIViewAnimationOptionBeginFromCurrentState;    [UIView animateWithDuration:animated ? 0.25 : 0.0 delay:0.0 options:options animations:^{        tableView.contentInset = contentInset;    } completion:^(BOOL finished) {    }];}

Call this method whenever a row is added:

- (void)updateContentInset {    NSInteger numRows=[self tableView:_tableView numberOfRowsInSection:0];    CGFloat contentInsetTop=_tableView.bounds.size.height;    for (int i=0;i<numRows;i++) {        contentInsetTop-=[self tableView:_tableView heightForRowAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];        if (contentInsetTop<=0) {            contentInsetTop=0;            break;        }    }    _tableView.contentInset = UIEdgeInsetsMake(contentInsetTop, 0, 0, 0);}

You can set a header in your table view and make it tall enough to push the first cell down. Then set the contentOffset of your tableView accordingly. I don't think there is a quick built in way to do this though.