如何向下滚动隐藏导航栏和工具栏?Swift(喜欢myBridge应用程序)

Lyn*_*Kay 25 uinavigationbar uitoolbar ios swift

我想在向下滚动页面时隐藏工具栏和导航栏.当我向上滚动时返回它.这怎么可能?

我该如何检测阻力?我是否使用平移手势或滚动视图?

Joe*_*Joe 80

试试这个简单的方法:测试 Swift 3

    func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {

    if(velocity.y>0) {
        //Code will work without the animation block.I am using animation block incase if you want to set any delay to it.
        UIView.animate(withDuration: 2.5, delay: 0, options: UIViewAnimationOptions(), animations: { 
            self.navigationController?.setNavigationBarHidden(true, animated: true) 
            self.navigationController?.setToolbarHidden(true, animated: true)
            print("Hide")
        }, completion: nil)

    } else {
        UIView.animate(withDuration: 2.5, delay: 0, options: UIViewAnimationOptions(), animations: { 
            self.navigationController?.setNavigationBarHidden(false, animated: true)
            self.navigationController?.setToolbarHidden(false, animated: true)
            print("Unhide")
        }, completion: nil)    
      }
   }
Run Code Online (Sandbox Code Playgroud)

输出: Updated

在此输入图像描述

注意:如果你从这个VC传递任何数据到嵌入另一个VC navigationController.你可能需要unhideNavigationBar.

  • @Joe 非常适合我!我将其缩短为“self.navigationController?.setNavigationBarHidden(velocity.y &gt; 0,animated:true)”,因为我只有导航栏需要处理! (2认同)
  • 好的解决方案,但是如果您有很长的列表并用手指停止滚动,则会出现导航栏。一个放置“if else”子句,其中velocity.y &lt; 0 - 涵盖这种情况。此外,当点击 statusBar 和 scrollToTop 激活时,我找不到如何修复错误 - scrollViewWillEndDragging 没有检测到它。 (2认同)

Huu*_*yen 19

很容易做到这一点:

navigationController?.hidesBarsOnSwipe = true
Run Code Online (Sandbox Code Playgroud)

  • 仅使用此方法,导航栏将永远消失 (4认同)

rak*_*hNS 11

在我看来,在 Tableview 中处理导航栏的正确方法如下。如果我们在 Tableview 中有节标题,这将适用。

func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView) {
   if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0 {
      navigationController?.setNavigationBarHidden(true, animated: true)

   } else {
      navigationController?.setNavigationBarHidden(false, animated: true)
   }
}
Run Code Online (Sandbox Code Playgroud)


viv*_*vat 5

您可以self.navigationController?.hidesBarsOnTap = true在viewDidAppear 中尝试也可以在滑动时使用隐藏.


Lyn*_*Kay 5

谢谢大家,我采用的方式是使用AMScrollingController.

https://github.com/andreamazz/AMScrollingNavbar

它已针对Swift 3进行了更新