【问题标题】:How to hide the navigation bar and toolbar as scroll down? Swift (like myBridge app)向下滚动时如何隐藏导航栏和工具栏? Swift(如 myBridge 应用程序)
【发布时间】:2017-04-01 18:25:31
【问题描述】:

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

我将如何检测阻力?我是使用平移手势还是滚动视图向下?

【问题讨论】:

标签: ios swift uinavigationbar uitoolbar


【解决方案1】:

Swift 用户界面

extension UINavigationController {
    override open func viewDidLoad() {
        super.viewDidLoad()
    hidesBarsOnSwipe = true
    // other customizations
    navigationBar.tintColor = .white
 }
}

【讨论】:

    【解决方案2】:

    在我看来,在 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)
       }
    }
    

    【讨论】:

    • 我改变了我使用的方法,效果很好,它很简单,代码更少。谢谢
    • 完美运行,应该被接受的答案
    【解决方案3】:

    我在滚动视图中实现了这一点,因为我使用了UITableViewUICollectionView 以外的组件,不确定它是否适合您,但它对我来说非常适合:

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let totalTop = (UIApplication.shared.statusBarFrame.size.height ?? 0) + (self.navigationController?.navigationBar.frame.height ?? 0)
        let shouldHideNavBar = scrollView.contentOffset.y > -(totalTop - 20) // 20 is an arbitrary number I added to compensate for some of scrolling
        navigationController?.setNavigationBarHidden(shouldHideNavBar, animated: true)
    }
    

    【讨论】:

      【解决方案4】:

      Swift 5 Xcode 10.3

      override func viewWillAppear(_ animated: Bool) {
          super.viewWillAppear(animated)
          navigationController?.hidesBarsOnSwipe = true
        }
      override func viewWillDisappear(_ animated: Bool) {
          super.viewWillDisappear(animated)
          navigationController?.hidesBarsOnSwipe = false
        }
      

      【讨论】:

        【解决方案5】:

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

        【讨论】:

          【解决方案6】:

          这是一个很好的选择

          在用户滚动时轻松隐藏和显示视图控制器的导航栏/标签栏 https://github.com/tristanhimmelman/HidingNavigationBar

          import HidingNavigationBar
          
          class MyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
          
              var hidingNavBarManager: HidingNavigationBarManager?
              @IBOutlet weak var tableView: UITableView!
          
              override func viewDidLoad() {
                  super.viewDidLoad()
          
                  hidingNavBarManager = HidingNavigationBarManager(viewController: self, scrollView: tableView)
              }
          
              override func viewWillAppear(animated: Bool) {
                  super.viewWillAppear(animated)
          
                  hidingNavBarManager?.viewWillAppear(animated)
              }
          
              override func viewDidLayoutSubviews() {
                  super.viewDidLayoutSubviews()
          
                  hidingNavBarManager?.viewDidLayoutSubviews()
              }
          
              override func viewWillDisappear(animated: Bool) {
                  super.viewWillDisappear(animated)
          
                  hidingNavBarManager?.viewWillDisappear(animated)
              }
          
              //// TableView datasoure and delegate
          
              func scrollViewShouldScrollToTop(scrollView: UIScrollView) -> Bool {
                  hidingNavBarManager?.shouldScrollToTop()
          
                  return true
              }
          
              ...
          }
          

          【讨论】:

          • 我编辑了一些代码,@RichardTelford,你可以控制它
          【解决方案7】:

          很容易做到这一点:

          navigationController?.hidesBarsOnSwipe = true
          

          【讨论】:

          • 仅使用这种方法导航栏会永远消失
          • @Kappe 确保滚动视图或表格视图的顶部约束是超级视图,而不是安全区域或顶部布局指南。那应该可以解决它。
          • 这个更好,非常适合我的需要。
          • 那怎么再显示呢??
          【解决方案8】:

          谢谢大家,我使用的方法是使用 AMScrollingController。

          https://github.com/andreamazz/AMScrollingNavbar

          它已针对 Swift 3 进行了更新

          【讨论】:

            【解决方案9】:

            试试这个简单的方法:在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)    
                  }
               }
            

            输出: Updated

            注意:如果您将任何数据从这个 VC 传递到另一个嵌入了 navigationController 的 VC。您可能需要 unhide NavigationBar

            【讨论】:

            • 你好,这可以隐藏和取消隐藏它,但是你是怎么做到让状态栏下方的黄色部分的?
            • @MagoNicolasPalacios 通常当您隐藏 navigationBar.statusbar 背景变为透明。我将 viewController 背景颜色设置为黄色并将 tableview contentInset 顶部填充设置为 statusBar 框架高度(向下 20px)。
            • 我知道菜鸟问题。但是你在哪里以及如何调用这个函数呢?我的意思是 velocity 和 targetContentOffset 来自哪里?另外,您将滚动视图放置为父容器,并将表格视图放置在其中?
            • @Joe 非常适合我!我将其缩短为self.navigationController?.setNavigationBarHidden(velocity.y &gt; 0, animated: true),因为我只有导航栏需要处理!
            • 很好的解决方案,但是如果你有很长的列表并用手指停止滚动导航栏就会出现。一个 put "if else" 子句,其中 velocity.y
            【解决方案10】:

            你可以使用这几行代码:

            - (void)scrollViewDidScroll: (UIScrollView *)scroll {
                // UITableView only moves in one direction, y axis
                CGFloat currentOffset = scroll.contentOffset.y;
                CGFloat maximumOffset = scroll.contentSize.height - scroll.frame.size.height;
            
                // Change 10.0 to adjust the distance from bottom
                if (maximumOffset - currentOffset <= 10.0) {
                    self.navigationController?.hidden = YES;
                }
                else{
                    self.navigationController?.hidden = NO;
                }
            }
            

            【讨论】:

            • 问题被标记为 Swift,而不是 Objective-C。请以适当的语言发布答案。
            • 请使用 scrollViewWillEndDragging 委托方法
            • 它有帮助..我不在乎它是在快速的“标签”下回答的。如果它真的在乎,为什么不在每个客观的c“标签”下质疑那些快速的答案???为什么?
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-04-30
            • 1970-01-01
            • 1970-01-01
            • 2017-03-03
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多