2016-02-28 309 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