2017-05-30 3 views
2

問題は本当に簡単です。ナビゲーションバーの下のリスト(メニュー)をクリックすると(ナビゲーションバー上で)表示する方法がわかりません。Swift - アニメーションのあるナビゲーションバーの下のメニュー

私はこのイメージよりも同じことをしたいと思います: enter image description here

私はこれを実行しようとしました:appDelegateで

func doSomething(){ 

     let navigationBarHeight = self.navigationController?.navigationBar.frame.height ?? 0 
     print(navigationBarHeight) 
     let heightTotal = UIApplication.shared.statusBarFrame.height + navigationBarHeight 

     DispatchQueue.main.async(execute: { 
     appDelegate.infoView(message: "test", Yorigin: heightTotal, color: colorBlueFollow) 
     }) 
    } 

そして、この:

func infoView(message: String, Yorigin: CGFloat ,color: UIColor){ 
     if infoViewIsShowing == false{ 
     infoViewIsShowing = true 

//   let infoViewHeight = self.window!.bounds.height/14.2 
     let infoViewHeight = self.window!.bounds.height/4.2 
     let infoViewY = Yorigin - infoViewHeight 

     let infoView = UIView(frame: CGRect(x: 0, y: infoViewY, width: self.window!.bounds.width, height: infoViewHeight)) 
     infoView.backgroundColor = color 
     self.window!.addSubview(infoView) 

     let infoLabelWidth = infoView.bounds.width 
     let infoLabelHeight = infoView.bounds.height + UIApplication.shared.statusBarFrame.height/2 

     let infoLabel = UILabel() 
     infoLabel.frame.size.width = infoLabelWidth 
     infoLabel.frame.size.height = infoLabelHeight 
     infoLabel.numberOfLines = 0 

     infoLabel.text = message 
     infoLabel.font = UIFont(name: "HelveticaNeue", size: 11) 
     infoLabel.textColor = UIColor.white 
     infoLabel.textAlignment = .center 

     infoView.addSubview(infoLabel) 

     // Animate errorView 
     UIView.animate(withDuration: 0.2, animations: { 

      // Move down 
      infoView.frame.origin.y = Yorigin 

     }, completion: { (finished: Bool) in 
      if finished{ 

       UIView.animate(withDuration: 0.2, delay: 3, options: .curveLinear, animations: { 
        // move up 
        infoView.frame.origin.y = infoViewY 

       }, completion: { (finished: Bool) in 
        if finished { 
        infoView.removeFromSuperview() 
        infoLabel.removeFromSuperview() 
        self.infoViewIsShowing = false 
        } 
       }) 

      } 
     }) 


     } 
    } 

問題があります表示されているビューがナビゲーションバーの上を通過している、それは私が好きなエフェットではありません...

私はそれをどうやってできるのか考えていますか?

あなたはcocoapodsに見ていました事前

答えて

1

この行を

self.window!.addSubview(infoView) 

は、(ナビゲーションバーを含む)、ウィンドウ内の他のすべてのビューの上にそれを置きます。

ビューコントローラの階層を検索し、rootViewControllerから始めてビューを追加する必要があります。いくつかのオプションのため、この質問への回答で

ルック:iPhone -- How to find topmost view controller

+0

私のコードをAppDelegateから現在のView Controllerに移動し、self.windowを変更しました。 self.viewに。それは単純なahaだった! – KevinB

0

あなたはのUIViewControllerは、あなたがそれを設定することができますサブクラス化と仮定は次のようにプロパティ「navigationItem」です。ナビゲーションコントローラ上のタイトルテキストを設定する一般的な方法の

self.navigationItem.title = "Your text" 

一つ下のテーブルビューのデリゲートメソッドをオーバーライドして、サブクラスのViewControllerのNAVアイテムに対する細胞のtextLabelのテキストですコピーすることです。

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     self.navigationItem.title = tableView.cellForRow(at: indexPath)?.textLabel?.text 
    } 
関連する問題