28

in iOS, is it possible to add a button next to the default back button (the one that is automatically created when you use the method pushViewController)

I would like to add a button here !

I've tried to use the methods setLeftBarButtonItems and setBackBarButtonItem but in vain - whenever I set the left bar button items, the default back button disappears.

Thanks for your help!

Arnaud
  • 7,259
  • 10
  • 50
  • 71
  • 1
    You can use Titleview to customize. Create a view with a Button and Title and assign that too the titleview of navigationbar. – iphonic Sep 12 '13 at 09:41
  • possible duplicate of [Add another button next to the "back" button on the left of a UINavigationBar](http://stackoverflow.com/questions/6561366/add-another-button-next-to-the-back-button-on-the-left-of-a-uinavigationbar) – Vinodh Sep 12 '13 at 09:48

2 Answers2

79

Just do

self.navigationItem.leftItemsSupplementBackButton = YES;
self.navigationItem.leftBarButtonItems = @[item1, item2];
Thomas Keuleers
  • 6,075
  • 2
  • 32
  • 35
  • 1
    this is why I love SO. +1 for `setLeftBarButtonItems` method. I would have achieved the same thing using titleview. Thanks for the tip. – Naz Mir Sep 12 '13 at 09:54
  • 2
    +1: Yep, that's what this property is intended for. From `UINavigationBar.h` header, `By default, the leftItemsSupplementBackButton property is NO. In this case, the back button is not drawn and the left item or items replace it. If you would like the left items to appear in addition to the back button (as opposed to instead of it) set leftItemsSupplementBackButton to YES.` – JRG-Developer Sep 12 '13 at 09:54
  • +1 for `leftItemsSupplementBackButton`. I hadn't seen that method before. Good call. – Zack Brown Sep 12 '13 at 10:15
  • Perfect! nice and not a hack! – sachadso Apr 21 '15 at 14:55
  • Is there a way to apply this globally in AppDelegate? – Michael Voccola Dec 28 '16 at 02:48
3

I haven't tested the following code, but it should work as long as the backBarButtonItem has been initialised.

[[self navigationItem] setLeftBarButtonItems:[NSArray arrayWithObjects:[[self navigationItem] backBarButtonItem], [[UIBarButtonItem alloc] initWithTitle:@"Custom" style:UIBarButtonItemStylePlain target:self action:@selector(action:)], nil]];\

Essentially, you're setting the entire left bar button item array from scratch but providing the back button along with your own custom button.

Zack Brown
  • 5,990
  • 2
  • 41
  • 54
  • Thanks for your answer, but it does not work - `[[self navigationItem] backBarButtonItem]` returns `nil` in `viewWillAppear` – Arnaud Sep 12 '13 at 09:58
  • 1
    @Naz there is `setLeftBarButtonItems` in my question also :-) – Arnaud Sep 12 '13 at 10:01