I want to put buttons over UITableView
which stays on same position of screen, won't scroll with UITableView
. When I tried to addSubview
buttons, they are displayed but scrolls with UITableView
.
Reason to put buttons over UITableView
is to let user operate some action on it. I'm not talking about having buttons in cell. They are floating buttons which never moves their position.
I'm using UITableViewController
, and I don't want to use header or footer for this purpose. And also another some bar (UIToolbar
for instance) is not an option for me.
Thanks in advance.
You need to set the UITableView scroll to false , tableview. scrollEnabled = false; tableview.
To scroll to the top of our tableview we need to create a new IndexPath . This index path has two arguments, row and section . All we want to do is scroll to the top of the table view, therefore we pass 0 for the row argument and 0 for the section argument. UITableView has the scrollToRow method built in.
Since a scrollView has a panGesture we can check the velocity of that gesture. If the tableView was programmatically scrolled the velocity in both x and y directions is 0.0. By checking this velocity we can determine if the user scrolled the tableView because the panGesture has a velocity.
You can do it also with UITableViewController
(no need for ordinary UIViewController
, which nests your table or VC)
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]; }
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
In case you are using navigation controller you can add the view to it:
[self.navigationController.view addSubview:yourView];
Example:
UIButton *goToTopButton = [UIButton buttonWithType:UIButtonTypeCustom]; goToTopButton.frame = CGRectMake(130, 70, 60, 20); [goToTopButton setTitle:@"Scroll to top" forState:UIControlStateNormal]; [goToTopButton addTarget:self action:@selector(goToTop) forControlEvents:UIControlEventTouchUpInside]; [goToTopButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [goToTopButton.layer setBorderColor:[[UIColor whiteColor] CGColor]]; goToTopButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:13]; [self.navigationController.view goToTopButton];
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