Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 846
Plan B Avatar asked Feb 04 '13 15:02

Plan B


People also ask

How do I make a Tableview not scrollable?

You need to set the UITableView scroll to false , tableview. scrollEnabled = false; tableview.

How do I scroll to top 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.

How can I tell if a Tableview is scrolling?

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.


2 Answers

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

like image 188
Peter Lapisu Avatar answered Nov 16 '22 00:11

Peter Lapisu


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]; 
like image 32
Yossi Avatar answered Nov 16 '22 00:11

Yossi