简体   繁体   中英

UINavigationController TitleView not displayed from ViewController NavigationItem

Whenever I add a viewController to a navigationController while in landscape the title view appears on certain views but not on others. ie: I have a navigation controller, add 3 view controllers, first two show titleview appropriately, third one doesn't show one at all. But the navigation controller grabs the titleview from the ViewController like it's supposed to, I wrote the value of it to the console and it is correct, but it just doesn't show on the screen for whatever reason. Any ideas?

Oh yeah works perfectly while in portrait orientation.

Here's another fun part, if I push the trouble view controller into the navigationController in landscape the titleView isn't there, then without any user interaction, I rotate the device back to portrait and the titleView appears, then I rotate the device back to landscape and it stays!

It's like the drawing of my TitleView was blocked even though I used InvokeOnMainThread. Nothing is running in the main thread (or anywhere for that matter) during that call.

Here's my structure:

Window
  TabBarController 
    NavigationController 
      ViewController
    NavigationController
      ViewController

Here's my order of operations:

  1. Create View Controller
  2. Add Title view to view controller
  3. Push View Controller onto NavigationController (InvokeOnMainThread)

Have you tried setting the controller title after the controller is pushed? This kind of behavior happens to me and the way to make sure the title appears is to mandatory set the navBar title in the viewDidLoad or viewWillAppear method as follows:

self.navigationController.navigationBar.topItem.title = @"The title";

or

self.navigationItem.title = @"The Title";

Other thing that happened to me is to set the leftBarButton or RightBarButton of a navigation bar without success in the viewDidLoad method, but they appear correctly when setting the bar buttons in the viewWillAppear method.

Hope this helps.

I think your problem maybe that when your function is called, the navigation item is nil. So when you call self.navigationITem.title, it do nothing. Later, when the view is rotated, the navigationItem is not nil anymore so changing the title works.

If you do the code in ViewDidLoad function beware that ViewDidLoad is called the first time someone calls viewController.view and not the first time the view is displayed. So the view may not be in a navigationController yet.

For example, this can happend if you do :

viewController.view.backgroundColor = ... ; 
[navigationController pushViewController:viewController]

The first line will call ViewDidLoad even if the controller is not in a navigationController yet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM