How to put buttons over UITableView which won’t scroll with table in iOS

You can do it also with UITableViewController (no need for ordinary UIViewController, which nests your table or VC)

UPDATED iOS11+ using SafeAreas

you want to use autolayout to keep the view on bottom

{
    [self.view addSubview:self.bottomView];
    [self.bottomView setTranslatesAutoresizingMaskIntoConstraints:NO];
    UILayoutGuide * safe = self.view.safeAreaLayoutGuide;
    [NSLayoutConstraint activateConstraints:@[[safe.trailingAnchor constraintEqualToAnchor:self.bottomView.trailingAnchor],
                                              [safe.bottomAnchor constraintEqualToAnchor:self.bottomView.bottomAnchor],
                                              [safe.leadingAnchor constraintEqualToAnchor:self.bottomView.leadingAnchor]]];
    [self.view layoutIfNeeded];
}

and to make sure view is always on top

- (void)viewDidLayoutSubviews {

    [super viewDidLayoutSubviews];

    [self.view bringSubviewToFront:self.bottomView];

}

THE PREVIOUS OLD SOLUTION

By scrolling the table, you need to adjust the position of the “floating” view and it will be fine

If you are in a UITableViewController, just

If you want to float the view at the top of the UITableView

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGRect frame = self.floatingView.frame;
    frame.origin.y = scrollView.contentOffset.y;
    self.floatingView.frame = frame;

    [self.view bringSubviewToFront:self.floatingView];
}

If you want float the view on the bottom of the UITableView

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGRect frame = self.floatingView.frame;
    frame.origin.y = scrollView.contentOffset.y + self.tableView.frame.size.height - self.floatingView.frame.size.height;
    self.floatingView.frame = frame;

    [self.view bringSubviewToFront:self.floatingView];
}

I believe thats the real answer to your question

Leave a Comment