UIBarButtonItem is blue instead of image

2019-02-19 07:33发布

问题:

Here is what I have in my viewDidLoad method:

self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "back")
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "back")
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)

I am trying to have a custom icon image for my back button in my navigation controller. Instead of the image showing in it's original format, I am seeing the image in blue. How do I get the image to show properly?

回答1:

You can use renderingMode for your custom image with AlwaysOriginal mode. See below code for solved it.

self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "back")?.imageWithRenderingMode(.AlwaysOriginal)
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "back")?.imageWithRenderingMode(.AlwaysOriginal)

Hope that helps!



回答2:

Quickest solution

.navigationController?.navigationBar.tintColor = UIColor.redColor()

Or you can create custom barButton item, with custom view



回答3:

Set the tintColor property of the barbuttonItem.

self.navigationItem.backBarButtonItem.tintColor = "any color"