Anh
Anh

Reputation: 6533

UINavigationController's back button disappears?

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?

Upvotes: 11

Views: 4974

Answers (3)

Shawn Frank
Shawn Frank

Reputation: 5143

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

Upvotes: 1

Jeggy
Jeggy

Reputation: 1650

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

self.navigationItem.leftItemsSupplementBackButton = true

Upvotes: 1

Anh
Anh

Reputation: 6533

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.

Upvotes: 24

Related Questions