开发者

UINavigationController Back Button not visible, but works

开发者 https://www.devze.com 2022-12-24 16:19 出处:网络
I have a scenario where my UINavigationController is missing the back button (left button) but tapping the left button still seems to work.

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 开发者_StackOverflowthat'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.


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.


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.


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;


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 Back Button not visible, but works

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

UINavigationController Back Button not visible, but works

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号