2017-07-15 95 views
1

我正試圖讓我的導航欄隱藏,當我向上滑動並重新出現時向下滑動。我打印功能的作品,但我不知道爲什麼我的導航欄不隱藏。另外我怎樣才能得到頂部是一個圖像和一個標誌隱藏在滑動。 enter image description here爲什麼我的導航欄不能在滑動時隱藏?

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

    if(velocity.y>0) { 

     UIView.animate(withDuration: 2.5, delay: 0, options: UIViewAnimationOptions(), animations: { 
      self.navigationController?.setNavigationBarHidden(true, animated: true) 


      print("Hide") 
     }, completion: nil) 

    } else { 
     UIView.animate(withDuration: 2.5, delay: 0, options: UIViewAnimationOptions(), animations: { 
      self.navigationController?.setNavigationBarHidden(false, animated: true) 

      print("Unhide") 
     }, completion: nil)  
    } 
} 
+0

確保self.navigationController不爲零。如果不存在,代碼將無聲無息地失敗。 –

+0

我在哪裏檢查? @DuncanC – Riccardo

+0

更改您的打印語句以讀取'print(「關於隱藏。navigationController = \(self.navigationController)」)' –

回答

0

你可以嘗試隱藏導航欄這樣

navigationController?.hidesBarsOnSwipe = true 

這會自動隱藏,當你向上滾動欄,顯示欄,當你向下滾動。它也將處理調整大小和動畫。與您的代碼

一個問題是:scrollViewWillEndDragging不會被觸發,直到你完成拖動,並且可以在一個拖動動作來觸發多次

+0

不起作用 – Riccardo