10

Assume that I have a ViewControllerA that points to ViewControllerB, I can set the title of Back button in ViewControllerB via this in ViewControllerA:

navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)  //set title to blank
navigationController?.pushViewController(destinationVC, animated: true)

However, how can I replace the image? I tried this but I still only see the default "Back" image, instead of a custom image:

navigationItem.backBarButtonItem = UIBarButtonItem(image: UIImage(named: "back-toolbar"), style: UIBarButtonItemStyle.Plain, target: nil, action: nil)

enter image description here

I want to use this image:

enter image description here

After reading many other SO posts, I don't want to implement a leftButtonItem for each destination view like ViewControllerB and have to manage the back Action and swipe left gesture.

I also can't set the image in AppDelegate because I use one image for some views and another image for other views.

UPDATE:

Per @rounak's tip, I was able to set the backBarButton, but the position seems wrong. Any way to adjust it?

enter image description here

netwire
  • 7,108
  • 12
  • 52
  • 86
  • Please explain the actual results you're seeing and how these differ from the actual results you want. "it doesn't work" is not a sufficient description of your problem. What's "not working" about it? Does it crash? Does it compile? Are their warnings or errors? Does it run fine and simply display the standard look versus whatever your image is? Does it launch missiles? – nhgrif May 02 '15 at 13:07
  • Sorry, I added a screenshot. – netwire May 02 '15 at 13:10

2 Answers2

20

You can just set the backIndicatorImage:

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

This will apply the indicator image only for that navigation controller, rather than all navigation controllers in your app. You can change this image inside your view controller in viewWillAppear/viewWillDisappear for view controllers that need a different back image.

rounak
  • 9,217
  • 3
  • 42
  • 59
  • Thanks @rounak, that worked. Is there a way to adjust the image size and position? The size and position seems a little off. PS. Good tip on resetting it on `viewWillDisappear`. – netwire May 02 '15 at 15:23
  • @Dean iOS typically does a good job at positioning the back button, but if you find that unsatisfactory, you can try adding a little margin to the image to move it to the side you want. – rounak May 02 '15 at 15:27
  • What's the size of image I should create? iOS doesn't seem to be resizing the image. – netwire May 02 '15 at 15:28
  • @Dean HIG recommends that bar button images be 22x22 http://stackoverflow.com/a/1591139/424406 – rounak May 02 '15 at 15:30
  • What about removing the back button text? – Rodrigo Ruiz Jun 06 '17 at 21:06
3

There's no need to set the leftButtonItem for each view controller. You can replace the image via the appearance property selectors of UINavigationBar which will set the default back indicator image for the whole app.

Here's an example in Objective-C, i'm sure you can easily find it's Swift equivalent:

[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@"back-toolbar.png"]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"back-toolbar.png"]];
Artal
  • 8,933
  • 2
  • 27
  • 30
  • Hi Artal, unfortunately I can't do that. I have some views that use one image for back button and other views that use another image. – netwire May 02 '15 at 14:21
  • @Dean I see. But, you can still use this method multiple times and switch the image to the correct one before showing certain view controllers, and change it back when they are dismissed. – Artal May 02 '15 at 14:26
  • If you do just that, the back button text will still be there. – Rodrigo Ruiz Jun 06 '17 at 21:05