Shawn Frank
Shawn Frank

Reputation: 5143

iOS Custom Navigation bar back button title disappears on tapping

Hello StackOverflow community,

I have a rather curious case of a customized back button on my navigation controller's navigation bar disappearing on interaction.

Some additional information is that my code is not using Storyboard and I use UIKit - everything including the UI has been build programatically.

This is my set up:

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)
}

UINavigationController custom back button

UINavigationController UINavigationBarButton back button disappears

When I tap on the back arrow after the label disappears, it works and takes me back to the previous screen, however, why would this disappearance occur ?

What have I tried I have tried many suggestions on stack overflow such as:

I can confirm that these are not the issue as I have a valid title and never hide the back button.

Here are some resources I have checked before deciding that none of them work for my case and I asked the question on here:

UINavigationController Back Button not visible, but works

UINavigationController's back button disappears?

Navigation title disappears after use of custom back button

navigation title on previous screen disappear when back button pressed

Missing Navigation's or Back Button's Title When Push ViewControllers in Succession

Any ideas as to why this back button text disappears ?

Upvotes: 3

Views: 1057

Answers (1)

Shawn Frank
Shawn Frank

Reputation: 5143

So after a lot of trial and error, this is what seemed to have worked for me.

I needed to even specify what the attributes are of the highlighted state.

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

After this, the text never disappeared and everything worked as intended.

I am not marking this as the answer for now as perhaps someone has a better "right" way to do this.

In any case I am putting this answer here if anyone else might be facing a similar scenario.

Upvotes: 7

Related Questions