Reputation: 614
I currently have a navigation bar that is getting a little bulky. In the image below, the search button displays a search bar, which also brings up a cancel button with it. What I would like to do here is have one button that slides in/displays the current right bar button items.
The search button was created completely in the storyboard, but the category button was added programmatically using a segue identifier I set in the storyboard due to bar item limitations in the storyboard. First up, we have the code that corresponds with the buttons and then the screen shot. Remember this is for the right bar button items.
Button Initializedself.categoryButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Categories", @"categories button") style:UIBarButtonItemStylePlain target:self action:@selector(categoryButtonDidPress:)];
self.navigationItem.rightBarButtonItems =[self.navigationItem.rightBarButtonItems arrayByAddingObject:self.categoryButtonItem];
Delegate methods for buttons
-(void) categoryButtonDidPress:(UIBarButtonItem *)sender {
[self performSegueWithIdentifier:@"categorySegueTable" sender:nil];
}
- (IBAction)searchButtonPressed:(id)sender {
self.navigationItem.titleView = self.searchBarTable;
self.searchBarTable.showsCancelButton = NO;
if (self.searchBarTable.hidden == YES) {
self.searchBarTable.hidden = NO;
}
}
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
[self.searchBarTable resignFirstResponder];
self.searchBarTable.hidden = YES;
}
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
self.searchBarTable.showsCancelButton = YES;
}
Upvotes: 1
Views: 439
Reputation: 3513
Instead of adding two buttons to the right side, you may find it easier to just add a segmentedController
to the right with 2 segments (buttons) it will reduce the space and make it easier to manage. Also, you may want to replace the word Categories with an icon to help compress the right side.
Upvotes: 1