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?
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
These two lines with do the trick. Set leftItemSupplementBackButton
to true, and then add leftBarButtonsItems
.
self.navigationItem.leftItemsSupplementBackButton = true
self.navigationItem.leftBarButtonItems = [barButton]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With