-->

Adding Navigation Item in UIViewController

2019-09-08 20:02发布

问题:

I'm trying to add UINavigationItem to my UIViewController as mentioned this answer. However the Navigation bar is not showing in my view. I tried even adding an outlet and setting the title programmatically and also adding the NavigationItem in two different places. Still it doesn't show. This view controller is embedded in a TabBarController. WHat am I missing here?

Thanks is advance.

回答1:

This view controller is embedded in a TabBarController

But is it embedded, first and foremost, in a UINavigationController? If not, there will be no navigation bar that automatically appears and that automatically uses your view controller's navigation item.

If you don't want to use a UINavigationController (because you have no navigation to do), then you can add a navigation bar manually. But in that case your view controller's navigation item will not be used automatically to populate the navigation bar; you must populate it manually.

Typically, people do use a UINavigationController, even if there is no navigation to do, just to get this automatic behavior - to show the navigation bar and to populate it automatically.

[NOTE: The fact that you have told Interface Builder to show a navigation bar for this view controller, as if it were in a navigation controller, is irrelevant; that won't cause you to get any navigation bar when the app runs.]