Here's my code for removing the back UIBarButtonItem of a navigation bar and replacing it with a cancel button:
UIBarButtonItem *cancelButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.hidesBackButton = YES;
self.navigationItem.leftBarButtonItem = cancelButtonItem;
Every example I've seen online doesn't hide the backButton before replacing it with a custom item. I may be wrong but it just seems like one unneeded line of code.
Did you try using UIBarButtonSystemItemCancel
instead of UIBarButtonItemStylePlain
in the style of your UIBarButtonItem
?
Try this,
UIBarButtonItem* cancelButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelAction)];
self.navigationItem.leftBarButtonItem = cancelButton;
Also if you have used self.navigationItem.backBarButtonItem
in your parentViewController
, it will show that button in the childViewController's leftBarButtonItem by default, if your custom leftBarButton in the childViewController is not added properly.