2017-05-30 54 views
2

我的問題很簡單,我不知道如何顯示導航欄下面的列表(菜單),當我點擊它(在導航欄上)。Swift - 導航欄下面的菜單與動畫

我願做同樣的事莫過於此圖片: enter image description here

我試着這樣做:

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) 
     }) 
    } 

而這的appDelegate:

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 
        } 
       }) 

      } 
     }) 


     } 
    } 

的問題是顯示的視圖正在通過導航欄上方,這不是我想要的效果...

你知道我該怎麼做嗎?

非常感謝你提前

回答

1

此行

self.window!.addSubview(infoView) 

把它放在的窗口中的所有其他視圖(包括導航欄)之上。

您應該搜索視圖控制器層次結構,從rootViewController開始查找要添加視圖的層級。

看在回答這個問題的一些選項:iPhone -- How to find topmost view controller

+0

感謝您的回答,我將我的代碼從AppDelegate移至當前的視圖控制器,並且我更改了self.window! self.view。就是那麼簡單啊! – KevinB

0

假設你的子類的UIViewController,那麼你可以設置它的屬性「navigationItem」如下。

self.navigationItem.title = "Your text" 

一個共同的方式設置導航控制器上的標題文字是覆蓋下面的tableview委託方法,並複製它的單元格爲textLabel的文本到子視圖控制器的導航項目。

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