-1

I am using a custom image asset for my navigation item left bar button item, but the wrong image is being rendered. In my Assets.xcassets folder, I have this image loaded:

expected back button image

And in my viewWillAppear method for the view controller, I execute this code:

self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: self, action: #selector(self.backButtonSelected))

But when I run this build, I am getting this image instead:

actual back button image

I've looked everywhere and can't find this image anywhere in my code, and this isn't even Apple's default back image.

Just to note: this use to not be the case and the correct image would be rendered. Not exactly sure how long ago this issue began, but must have been around the release of Xcode 10.2

2
  • try renaming you image and see if that fixes it
    – arvidurs
    Jun 19, 2019 at 18:57
  • I was planning on doing that, but I'm still curious about the root of the problem.
    – BlondeSwan
    Jun 19, 2019 at 18:58

2 Answers 2

0

Try to define a button and use it in UIBarButtonItem initializer, it should be:

let btn = UIButton(type: .custom)
btn.setImage(UIImage(named: "back"), for: .normal)
btn.addTarget(self, action: #selector(self.backButtonSelected), for: .touchUpInside)
let item = UIBarButtonItem(customView: btn)

self.navigationItem.leftBarButtonItem = item

But your problem could be your image name. Before trying my solution try to change image name to arrow_back or whatever you prefer.

0

can't find this image anywhere in my code

It isn't going to be in your code. It is in your asset catalog or in the app bundle (look through the Project Navigator for something called back.png). If it's in the app bundle, it will override the asset catalog.

In any case it is certainly not appearing out of nowhere. Trust me, you've got this image in your project, somewhere.

2
  • Wow, okay. I can't find it anywhere in by code base. Better?
    – BlondeSwan
    Jun 19, 2019 at 19:22
  • I don't know what those words mean. — Think of this as a file. Do a global search on your computer for something called back.png. You'll find it.
    – matt
    Jun 19, 2019 at 19:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.