10
votes

I notice something strange happens to one of my view controller: the back button disappears, yet it's possible to go back to previous view controller by tapping the top left corner (i.e where the button should reside).

In my entire file there's no line that set self.navigationItem.hidesBackButton to YES; also NSLog prints 0 as self.navigationItem.hidesBackButton's value in viewDidLoad.

This occurs in both the simulator and real device. Any ideas?

3
I had the same problem, but tapping in the top left corner did not have any effect, yet the cause was the same. - Erik B
Once I have my <Back button disappeared because I set self.navigationItem.hidesBackButton = NO; and forgot to call self.navigationItem.hidesBackButton = YES; - Andrey Solovyov

3 Answers

24
votes

Oh dear. In the implementation of the previous view controller, I accidentally set self.title to @"", which causes this annoying bug.

Remove this line solves the problem.

0
votes

I had a recursive navigation controller, and this also happened to me, I used this code to fix it:

self.navigationItem.leftItemsSupplementBackButton = true
0
votes

Just in case someone is facing this issue with a custom back button and the above fixes did not work, here is a similar issue I faced with a different solution.

I had a customized back button with text that 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