2016-02-28 310 views
2

我有一個現有的UITableViewController,我已經嵌入在NavigationController。但是,當我呈現視圖時,導航欄並未顯示。導航欄添加導航控制器後不顯示

呈現TableViewController(它的故事板ID是:SelectServicesController):

if let selectServicesController = self.storyboard?.instantiateViewControllerWithIdentifier("SelectServicesController") as? UITableViewController { 
    self.navigationController?.presentViewController(selectServicesController, animated: true, completion: nil) 
} 

enter image description here

這是個什麼樣子,當我建立一個像(導航欄不顯示):

enter image description here

+0

你在你的項目中的其他導航控制器? –

+0

@DanielLeonard是的。這是一個單獨的導航控制器。 – Onichan

+0

雅但它可以影響它,因爲它是一個父導航控制器。你也有你的表視圖限制在其超級視圖的頂部。 –

回答

4

所以我只是這樣做,並在拳頭無法讓它顯示出來。然後想通了,你只需要選擇導航控制器,並將其設置爲✅is initial View Controller

這是你的故事板應該是什麼樣子 NavigationController setup

然後讓一切顯示了我添加到了我導航控制器正在呈現的視圖的viewDidLoad。這一步更可選。

self.navigationController?.navigationBar.barTintColor = UIColor.redColor() 
    self.navigationController?.navigationBar.tintColor = UIColor.blackColor() 
    self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.blackColor()] 
    navigationController?.navigationBar.hidden = false 

而這正是它看起來像

NavContorllerInAction

MMMM紅黑色希望幫助你。

+0

我試過這個,但是我得到一個navigationController返回nil的錯誤。 – Onichan

+0

雅所以我的答案將無法正常工作,因爲它完全是任務,因爲根本沒有導航控制器。 –

+0

這不提供問題的答案。要批評或要求作者澄清,請在其帖子下方留言。 - [來自評論](/ review/low-quality-posts/11442121) – Teepeemm

0

您直接呈現表視圖控制器,而不是其導航控制器。如果標記導航控制器作爲初始視圖控制器(打勾的「在初始視圖控制器」,在屬性檢查框),那麼你可以實例,並通過展示它:

if let selectServicesNavController = self.storyboard?.instantiateInitialViewController() as? UINavigationController { 
    // if you're pushing it onto an existing nav controller 
    self.navigationController?.presentViewController(selectServicesNavController, animated: true, completion: nil) 

    // if not (and this is probably the case), set the nav controller as your window's rootViewController 
    UIApplication.sharedApplication().keyWindow.rootViewController = selectServicesNavController 
} 
+0

問題是,我有另一個導航控制器。 – Onichan

0

我的猜測是Xcode中被忽略事實上,與下面的代碼展示您的表視圖控制器時,你的表視圖控制器嵌入導航控制器:

if let selectServicesController = self.storyboard?.instantiateViewControllerWithIdentifier("SelectServicesController") as? UITableViewController { 
    self.navigationController?.presentViewController(selectServicesController, animated: true, completion: nil) 
} 

相反,我會建議你修改最上面一欄下的模擬指標設置,以滿足您的需要或實例化導航控制器代替(後者是首選和推薦)

+0

我試圖添加不透明的頂部欄,但它仍然沒有顯示任何東西。 – Onichan

0

在你的代碼,更改此行

self.navigationController?.presentViewController(selectServicesController, animated: true, completion: nil) 

這個

self.presentViewController(selectServicesController, animated: true, completion: nil) 
+0

我已經嘗試在導航欄中添加故事板ID並代之以導航控制器。但視圖完全忽略了調用,並且不顯示導航控制器和tableviewcontroller。 – Onichan

+0

您需要從視圖控制器本身提供導航控制器,而不是導航控制器。 – paulvs

2

你呈現一個UITableViewController,它不具有導航控制器作爲父母(即使你的故事板有首先,你並沒有真正使用它)。

if let selectServicesController = self.storyboard?.instantiateViewControllerWithIdentifier("SelectServicesController") as? UITableViewController { 
    let navigationController = UINavigationController(rootViewController: selectServicesController) 
    self.navigationController?.presentViewController(navigationController, animated: true, completion: nil) 
} 

,或通過設置導航控制器作爲初始視圖控制器故事板的,然後調用它像這樣:

你可以做這樣的事情解決這個

if let selectServicesController = self.storyboard?.instantiateInitialViewController() { 
    self.navigationController?.presentViewController(selectServicesController, animated: true, completion: nil) 
} 
0

我在你的一個評論中閱讀了你希望以模態呈現表格視圖控制器,導航欄顯示。我們可以使用Storyboard來做到這一點。在應該以模態方式顯示此表格視圖控制器的視圖控制器中,從視圖控制器Ctrl +拖動到表視圖控制器的導航控制器。然後,選擇目前Modally。設置一個標識符爲segue。然後,在你對所呈現的表視圖控制器模態視圖控制器代碼,請致電:

self.performSegueWithIdentifier("YourSegueIdentifier", sender: nil) 

另一種方式來做到這一點,而無需使用任何代碼,如果該模式演示是由像一個觸發按鈕。然後,您可以按Ctrl +拖動該按鈕到導航控制器,然後選擇虛擬顯示。

1

我遇到了同樣的問題。我通過將segue更改爲嵌入了要顯示的View Controller的導航控制器來解決此問題。 enter image description here

希望這會爲你工作。 讓我知道這是不是一個壞習慣。

0

或者,它可能是這個!

我有同樣的問題:導航欄是顯示在故事板根視圖,但在運行模擬器的時候 - 有在意見上沒有導航欄。這解決了它:

導航控制器>導航欄>取消選中半透明(它在默認情況下選中)。這做了兩件事:

  1. 我的導航欄顯示在所有後續的視圖。
  2. 最頂端的子視圖現在在Y = 0,而不是Y = 64。

enter image description here