1
votes

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:

  • I start with creating a custom UINavigationController subclass to abstract away all of my navigation controller related customizations

  • Within this subclass, I have this method that get's called to customized the appearance of my back button

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)
}
  • The above customization, gives the following result which is what I desire with Gallery being the title of my previous screen

UINavigationController custom back button

  • When I tap towards the left most edge of the back button where the arrow is, the button works normally. However, if I tap on the label section of the back button, where the Gallery text is, the text disappears and keeps the user on the same screen.

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:

  • making sure no line in my code hides the back button explicitly
  • Checking that previous screen's title to an empty string at any point

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 ?

1
does it help when you set navigationBar‘s tint color?Mr.SwiftOak
thanks for the idea @Mr.SwiftOak - however, still the same resultShawn Frank

1 Answers

0
votes

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.