Hide NavigationBar when scrolling tableView in CollectionView?

前端 未结 6 1584
一整个雨季
一整个雨季 2020-12-05 11:47

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

相关标签:
6条回答
  • 2020-12-05 12:16

    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.

    0 讨论(0)
  • 2020-12-05 12:28

    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)
        }
    }
    
    0 讨论(0)
  • 2020-12-05 12:30

    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.

    0 讨论(0)
  • 2020-12-05 12:37

    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.

    0 讨论(0)
  • 2020-12-05 12:37

    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 讨论(0)
  • 2020-12-05 12:38
     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;
           }
    
    0 讨论(0)
提交回复
热议问题