13

I have collectionViewController and collectionViewCell include TableView.CollectionView is horizontal layout.I want hide navigationbar when scroll the tableView. Is there any idea about that.

muhammedkasva
  • 672
  • 4
  • 12
  • 20

7 Answers7

27

Since iOS 8 you can just use

self.navigationController?.hidesBarsOnSwipe = true

This requires of course that your ViewController is embedded in a NavigationController. All child VC of the NavigationController will inherit this behaviour, so you might want to enable/disable it in viewWillAppear. You can also set the respective flags on the navigation controller in the storyboard.

Screenshot of Navigation Controller in Storyboard

Benjohn
  • 13,228
  • 9
  • 65
  • 127
Andy
  • 866
  • 9
  • 14
18

You can use some git libraries for scrollable Navigation bar whenever you want to scroll your table view/ Scroll top to bottom / bottom to top it will automatically adjust you Navigation bar.

you can use here like this code for use this library like this

Swift

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    if let navigationController = self.navigationController as? ScrollingNavigationController {
        navigationController.followScrollView(tableView, delay: 50.0)
    }
}

Objective - C

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [(ScrollingNavigationController *)self.navigationController followScrollView:self.tableView delay:50.0f];
}

It having some delegate methods help for manage all this related to scroll and navigation.

AMScrollingNavbar click here for see

enter image description here

I think this is helpful for you.

Anand Nimje
  • 6,163
  • 4
  • 24
  • 43
16

Try this:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0 {
        navigationController?.setNavigationBarHidden(true, animated: true)
    } else {
        navigationController?.setNavigationBarHidden(false, animated: true)
    }
}
nao
  • 165
  • 1
  • 3
5

create a @property(assign, nonatomic) CGFloat currentOffset;

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    scrollView = self.collectionProductView;
   _currentOffset = self.collectionProductView.contentOffset.y;

}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{

    CGFloat scrollPos = self.collectionProductView.contentOffset.y ;

    if(scrollPos >= _currentOffset ){
        //Fully hide your toolbar
        [UIView animateWithDuration:2.25 animations:^{
            [self.navigationController setNavigationBarHidden:YES animated:YES];

        }];
    } else {
        //Slide it up incrementally, etc.
        [self.navigationController setNavigationBarHidden:NO animated:YES];
    }
}

Please don't forget to again paste [self.navigationController setNavigationBarHidden:NO animated:YES];

at - viewwilldisappear or whenever the controller is moving to another because this may cause next view controller navigation bar to disappear.

Vaibhav Gaikwad
  • 396
  • 5
  • 13
0
 func scrollViewDidScroll(_ scrollView: UIScrollView)
       {
           //  var navigationBarFrame   = self.navigationController!.navigationBar.frame
           let currentOffset = scrollView.contentOffset

           if (currentOffset.y > (self.lastContentOffset?.y)!) {
               if currentOffset.y > 0 {
                   initial = initial - fabs(CGFloat(currentOffset.y - self.lastContentOffset!.y))
               }
               else if scrollView.contentSize.height < scrollView.frame.size.height {
                   initial = initial + fabs(CGFloat(currentOffset.y - self.lastContentOffset!.y))
               }
           }
           else {
               if currentOffset.y < scrollView.contentSize.height - scrollView.frame.size.height {
                   initial = initial + fabs(CGFloat(currentOffset.y - self.lastContentOffset!.y))
               }
               else if scrollView.contentSize.height < scrollView.frame.size.height && initial < maxPlus {
                   initial = initial - fabs(CGFloat(currentOffset.y - self.lastContentOffset!.y))
               }
           }

           if (initial <= maxMinus){
               initial =  maxMinus
               self.tableviewTopConstrin.constant = 0
               UIView.animate(withDuration: 0.4, animations: {
                   self.view.layoutIfNeeded()
               })

           }else if(initial >= maxPlus){
               initial = maxPlus
               self.tableviewTopConstrin.constant = 70
               UIView.animate(withDuration: 0.4, animations: {
                   self.view.layoutIfNeeded()
               })
           }else{
           }
           self.lastContentOffset = currentOffset;
       }
Mahesh Joya
  • 353
  • 1
  • 6
0

Adding on top of nao's answer:

If scrollview height is not small enough, it will cause non scrollable scrollview when navigationbar hidden. And if scrollview becomes non scrollable, this function is not called and navigation bar is gone forever

func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let height = view.safeAreaLayoutGuide.layoutFrame.size.height
        let scrolled = scrollView.panGestureRecognizer.translation(in: scrollView).y
        if !(scrollView.visibleSize.height - height >= 90) {
            if  scrolled < 0 {
                navigationController?.setNavigationBarHidden(true, animated: true)
            } else {
                navigationController?.setNavigationBarHidden(false, animated: true)
            }
        }
    }
0

Other solutions have a bug in which navController appears if you lift your finger from the screen and hold it again...

This one works in a better way:

// Show/Hide the NavigationBar when scrolling
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView.contentOffset.y > -48 {
        navigationController?.setNavigationBarHidden(true, animated: true)
    } else {
        navigationController?.setNavigationBarHidden(false, animated: true)
    }
}
oskarko
  • 3,382
  • 1
  • 26
  • 26