0

I saw iOS floating button over table view regarding how to create a floating button. This is the way I create the button. The problem is that the button will scroll with the table view.

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self 
           action:@selector(aMethod:)
 forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button]; 

How can I fix that?

I suggest you use https://github.com/gizmoboy7/VCFloatingActionButton this link....hope it will work for you

Community
  • 1
  • 1
user3241911
  • 481
  • 6
  • 15

2 Answers2

5

Assign your view controller as the delegate of the table view and override scrollViewDidScroll:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if ([scrollView isEqual:self.tableView]) {
        self.floatingButton.transform = CGAffineTransformMakeTranslation(0, scrollView.contentOffset.y);     
    }
}
Anjaneyulu Battula
  • 1,910
  • 16
  • 33
Austin
  • 5,625
  • 1
  • 29
  • 43
1

Try this:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint origin = [scrollView contentOffset];
    [scrollView setContentOffset:CGPointMake(0.0, origin.y)];

    if ([scrollView.panGestureRecognizer translationInView:scrollView].y > 0)
    {
        NSLog(@"Now TableView Scrolling UP");
        // Your Custom Code
    } 
    else 
    {
        NSLog(@"Now TableView Scrolling Down");
        // Your Custom Code
    }
}
Rajesh Loganathan
  • 11,129
  • 4
  • 78
  • 90