Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a left bar button without overriding the natural back button?

I have a project that uses UIViewControllers embedded in navigation view controllers, so the back button is automatically set up for me whenever I segue into any detail of my tableviews.

Now I want to add an Edit button beside the back button. I've already put an Unassign button on the right side, and since the word "Unassign" is quite long, I'd rather fit the Edit button beside the back button on the left side.

I know I can programmatically add bar buttons with navigationItem.leftBarButtonItem = editButton but that overrides the back button.

I've also tried appending the editButton to the navigationItem.leftBarButtonItems array, but that doesn't work too (since the leftBarButtonItems array isn't set in the first place).

My next approach was to find the back button and manually add it to the leftBarButtonItems array, so I tried using navigationItem.leftBarButtonItems = [self.navigationItem.backBarButtonItem!, editButton], but that only adds a space between the editButton and the left side. No back button.

How do I add in a left bar button while leaving the back button untouched? Is there a more elegant way than manually coding a new back button with self.navigationController?.popViewControllerAnimated(true) and adding it to the leftBarButtonItems array?

like image 633
Jace Tan Avatar asked Aug 22 '16 05:08

Jace Tan


2 Answers

By default, the leftItemsSupplementBackButton property is false. 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 true.

@property(nonatomic) BOOL leftItemsSupplementBackButton

you can use something like this

navigationItem.leftBarButtonItem = editButton
//or
navigationItem.leftBarButtonItems = [editButton]

self.navigationItem.leftItemsSupplementBackButton = true
like image 105
Er. Khatri Avatar answered Nov 20 '22 12:11

Er. Khatri


These two lines with do the trick. Set leftItemSupplementBackButton to true, and then add leftBarButtonsItems.

self.navigationItem.leftItemsSupplementBackButton = true
self.navigationItem.leftBarButtonItems = [barButton]
like image 42
MrWaqasAhmed Avatar answered Nov 20 '22 14:11

MrWaqasAhmed