I am using an ABPeoplePickerNavigationController in my app and have overrode the navigation bar buttons to my own using UINavigationControllerDelegate.
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
navigationController.topViewController.searchDisplayController.searchBar.barStyle = UIBarStyleBlack;
navigationController.topViewController.navigationItem.leftBarButtonItem = nil;
navigationController.topViewController.navigationItem.rightBarButtonItem = nil;
UIBarButtonItem *cancelButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:@selector(cancel:)];
navigationController.topViewController.navigationItem.leftBarButtonItem = cancelButtonItem;
UIBarButtonItem *addButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(addItem:)];
navigationController.topViewController.navigationItem.rightBarButtonItem = addButtonItem;
}
This works fine. However, when I use the search controller and exit out of it, my top right button suddenly changes to a Cancel button (see image below). How can I fix this? Thanks in advance.
OK, just figured out how to fix this. I just added a notification to see when the keyboard will be hidden and added the button back to the navigation bar.
1) Declare and synthesize the property mainNavigationController:
@property UINavigationController *mainNavigationController;
2) In (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated, add:
mainNavigationController = navigationController;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onNotification:) name:UIKeyboardWillHideNotification object:nil];
3) Add the method onNotification:
-(void)onNotification:(NSNotification*)notification
{
UIBarButtonItem *addButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(addItem:)];
mainNavigationController.topViewController.navigationItem.rightBarButtonItem = addButtonItem;
}