7
votes

I have a scenario where my UINavigationController is missing the back button (left button) but tapping the left button still seems to work.

I found a similar problem posted here: UINavigationController's back button disappears? which was resolved by not setting the title to @"", but that's not my problem. Are there any other scenarios that would cause this behaviour?

UPDATE:

In case it matters here is my view hierarchy: My MainWindow contains a UINavigationView which first loads a UIViewController (this view contains a Map). When tapping on an annotation accessory button it then loads a UITableViewController. It's this view that should have the back button.

Thanks.

4
Maybe the back button's hidden property is set to NO??Jacob Relkin
give it an actual name instead: @"My VC" to test for sure. Also it is the previous VC not the new one which you must give a title to (just in case)Corey Floyd
Jacob - I don't think that's it. I've searched my code and I make no reference to the left button at all, never mind the hidden property. Corey - Good idea. I tried that, from both spots, and my title updates as expected, but still no luck with the button. I'll update my original questions with my view hierarchy in case that makes a difference.cagreen

4 Answers

16
votes

Oh Man! Ok, I found the answer in an article on iphonedevsdk.com, now defunct.

It turns out that my first view (the one with the map on it) didn't have a title set (in fact I hide the navigation bar because I didn't want to show it). Even if the title WAS set on a later view the SDK doesn't seem to care. I still don't understand 100% why I could tap in the area to get it to work.

So even though I hide the first navigation bar I still need to set the title of it.

That did it!

Thanks to everyone who tried to help.

2
votes

Other than the suggestions provided above. changing the Appearance of Navigation bar can also make Back button invisible.

UINavigationBar.appearance().tintColor = UIColor.white

Verify if you are not setting this tint color to something which blend with the background.

0
votes

Are you overriding the back button item -- not the same as the left button -- anywhere in your code? i.e.:

self.navigationItem.backBarButtonItem = ...

Are you setting the hidesBackButton property anywhere? i.e.:

self.navigationItem.hidesBackButton = YES;
0
votes

I had a similar (although not exactly the same) situation with a customized back button text was disappearing while the arrow could be seen UINavigationController custom back button disappears from NavigationBar

So if anyone is facing a similar situation with disappearing back button text on a customized back button, here is my scenario and fix.

I customized my back button inside a custom NavigationController class as follows:

private func customizeBackButton() {
    let backImage = UIImage(named: "BackButton")?.withRenderingMode(.alwaysOriginal)
    navigationBar.backIndicatorImage = backImage
    navigationBar.backIndicatorTransitionMaskImage = backImage
    
    UIBarButtonItem.appearance().setTitleTextAttributes([
      NSAttributedString.Key.foregroundColor: UIColor.panoStoryYellow,
      NSAttributedString.Key.font: UIFont(name: "Montserrat-SemiBold", size: 15)!
    ], for: .normal)
}

This gave me:

UINavigationController custom back button

Now when I tapped on the back button text, the text disappeared: UINavigationController UINavigationBarButton back button disappears

I made sure that I followed all the above answers such as setting titles making sure the tint color is valid etc. however this did not work.

In my case, I needed to set attributes even for the highlighted state of the back button as follows:

UIBarButtonItem.appearance().setTitleTextAttributes([
      NSAttributedString.Key.foregroundColor: UIColor.panoStoryYellow,
      NSAttributedString.Key.font: UIFont(name: "Montserrat-SemiBold", size: 15)!
], for: .highlighted)

After this, the back button text never disappeared