Hide NavigationBar when scrolling tableView in CollectionView?

感情迁移 提交于 2019-11-27 20:16:51
Andy

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.

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

I think this is helpful for you.

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)
    }
}

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.

 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;
       }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!