Hide NavigationBar when scrolling tableView in CollectionView?

余生长醉 提交于 2019-12-28 05:40:07

问题


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.


回答1:


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.




回答2:


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.




回答3:


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



回答4:


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.




回答5:


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



回答6:


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


来源:https://stackoverflow.com/questions/37986923/hide-navigationbar-when-scrolling-tableview-in-collectionview

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