【问题标题】:How to hide navigation bar while scrolling the table view which is inside the collection view如何在滚动集合视图内的表视图时隐藏导航栏
【发布时间】:2018-07-09 07:19:03
【问题描述】:

我有一个包含集合视图的视图控制器。在集合视图中,我有一个表格视图。每当我滚动表格视图时,导航栏都应该隐藏。

  1. 视图控制器
  2. 集合视图 --- 集合视图单元格
  3. 表格视图----表格视图单元格

如何隐藏导航栏?

【问题讨论】:

    标签: ios swift uitableview uinavigationcontroller uicollectionview


    【解决方案1】:

    您可以使用滚动视图委托方法来隐藏和显示导航栏。 检查以下方法

    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        self.navigationController?.setNavigationBarHidden(true, animated: true)
    }
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        if decelerate == false {
            self.navigationController?.setNavigationBarHidden(false, animated: true)
        }
    }
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }
    

    希望它对你有用。

    【讨论】:

    • 出现错误。 “'CustomCollectionViewCell' 类型的值没有成员 'navigationController'”,因为 tableview 存在于 collectionview 中。所以所有的 tabelview 委托和数据源方法都写在自定义 collectionview 单元格中。包括scrollviewdidscroll方法。
    • 在具有 UICollectionView 隐藏导航栏的 ViewController 上实现这些委托方法
    【解决方案2】:

    在具有 UITableView 的 collectionViewCell 上创建委托方法

    @objc protocol CollectionViewCellDelegate {
      func didScroll(scrollView: UIScrollView) 
      func didEndDecelerating()
      func didEndDragging(scrollView: UIScrollView)
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        self.delegate.didScroll(scrollView)
    }
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        self.delegate.didEndDragging(scrollView)
    }
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        self.delegate.didEndDecelerating()
    }
    

    并在具有 UICollectionView 的 ViewController 上实现这些委托方法以隐藏导航栏

    【讨论】:

      【解决方案3】:

      在这些方法中使用观察者来实现你的目标

      func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
          NotificationCenter.default.post(name: 
          NSNotification.Name("ScrollStart"), object: nil)
      }
      
      func scrollViewDidEndDragging(_ scrollView: UIScrollView, 
          willDecelerate decelerate: Bool) {
          NotificationCenter.default.post(name: 
          NSNotification.Name("ScrollEnd"), object: nil)
      }
      

      现在你想隐藏导航栏的地方在控制器的视图中添加以下代码 didload()

      NotificationCenter.default.addObserver(self, selector: 
           #selector(self.hideNavigationMethod), name: 
       NSNotification.Name("ScrollStart"), object: nil)
      

      类似地显示导航栏

      NotificationCenter.default.addObserver(self, selector: 
           #selector(self.showNavigationMethod), name: 
      NSNotification.Name("ScrollEnd"), object: nil)
      

      现在实现方法 hideNavigationMethod() 和 showNavigationMethod()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-06-04
        • 1970-01-01
        • 1970-01-01
        • 2019-12-25
        • 1970-01-01
        • 1970-01-01
        • 2012-07-07
        相关资源
        最近更新 更多