ios 在 CollectionView 中滚动 tableView 时隐藏 NavigationBar?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37986923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:32:20  来源:igfitidea点击:

Hide NavigationBar when scrolling tableView in CollectionView?

iosswiftuitableviewuicollectionviewuinavigationbar

提问by muhammedkasva

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.

我有 collectionViewController 和 collectionViewCell 包括 TableView.CollectionView 是水平布局。我想在滚动 tableView 时隐藏导航栏。有什么想法吗。

回答by Andy

Since iOS 8 you can just use

从 iOS 8 开始你就可以使用

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.

这当然需要您的 ViewController 嵌入到 NavigationController 中。NavigationController 的所有子 VC 都将继承此行为,因此您可能希望在viewWillAppear. 您还可以在情节提要中的导航控制器上设置相应的标志。

Screenshot of Navigation Controller in Storyboard

Storyboard 中导航控制器的屏幕截图

回答by Anand Nimje

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.

每当您想滚动表格视图/从上到下/从下到上滚动时,您都可以使用一些 git 库作为可滚动导航栏,它会自动调整您的导航栏。

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

目标-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

AMScrollingNavbar 点击这里查看

enter image description here

在此处输入图片说明

I think this is helpful for you.

我认为这对你有帮助。

回答by nao

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

回答by Vaibhav Gaikwad

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

创建一个@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];

请不要忘记再次粘贴[self.navigationController setNavigationBarHidden:NO animation:YES];

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

at - viewwilldisappear 或每当控制器移动到另一个时,因为这可能会导致下一个视图控制器导航栏消失。

回答by Mahesh Joya

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

回答by Yunus Güng?r

Adding on top of nao's answer:

在 nao 的回答之上添加:

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