1

In iOS 11 searchBar hides when scrolling tableView if we set to hide. how can I hide searchBar, navigationBar and tabBar when scrolling collectionView up ? And unhide them all when scrolling down ? Thanks for your help...

BatyrCan
  • 6,773
  • 2
  • 14
  • 23

2 Answers2

5
  1. Subclass UIScrollViewDelegate in your UIViewController (i.e. class ViewController: UIViewController, UIScrollViewDelegate { codes... })
  2. Implement scrollViewDidScroll Delegate method

    func scrollViewDidScroll(scrollView: UIScrollView) { 
        let pan = scrollView.panGestureRecognizer
        let velocity = pan.velocityInView(scrollView).y
        if velocity < -5 { 
            self.navigationController?.setNavigationBarHidden(true, animated: true) 
            self.navigationController?.setToolbarHidden(true, animated: true)
        } else if velocity > 5 {
            self.navigationController?.setNavigationBarHidden(false, animated: true)
            self.navigationController?.setToolbarHidden(false, animated: true)
        }
    }
    
mamba4ever
  • 2,662
  • 4
  • 28
  • 46
KTang
  • 340
  • 1
  • 17
  • Thanks a lot KTang. you saved me 2 days. thanx. – BatyrCan Feb 22 '18 at 11:14
  • Actually, there have many ways to do that. But I think this is the most flexible one since you can control what to do when the scrollView is scrolling up or down, not just simply hide the bars. Moreover, you can adjust the "-5" & "5" in the if-case to customize the performance, you can try to see what will happen. – KTang Feb 23 '18 at 07:58
0

Translating KTang's accepted answer to Objective-C:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    UIPanGestureRecognizer *pan = scrollView.panGestureRecognizer;
    CGFloat velocity = [pan velocityInView:scrollView].y;

    if (velocity < -5) {
        [self.navigationController setNavigationBarHidden:YES animated:YES];
        [self.navigationController setToolbarHidden:YES animated:YES];
    } else if (velocity > 5) {
        [self.navigationController setNavigationBarHidden:YES animated:YES];
        [self.navigationController setToolbarHidden:YES animated:YES];
    }
}
OffensivelyBad
  • 621
  • 8
  • 16