Reputation: 3275
I'm struggling with the UISearchDisplayController
and hope someone out there can help me out.
So basically I've got a UINavigationBar
which has a "Search"-Button in it. When someone taps "Search" the UISearchDisplayController
should get active and the UISearchBar should appear inside the UINavigationBar
. Next to the SearchBar I've implemented a "Cancel"-Button which deactivates the SearchDisplayController
and resets the UINavigationBar
back to it's standard with the "Search"-Button.
My first problem is the gap between my SearchBar
and the results-table or the gray overlay while the SearchDisplayController
is active.
The second problem is that I can't reset my rightBarButtonItem to the default icon (magnifying-glass) after tapping cancel. It's nothing happening even when I try to set [self.navigationItem setRightBarButtonItem:nil];
it just remains with the "Cancel" button.
Here is my implementation: (The UI elements are added through my storyboard)
- (IBAction)btnSearchClicked:(id)sender {
[self.navigationController.navigationBar addSubview:self.searchController.searchBar];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelEditing)];
[self.searchController.searchBar setFrame:CGRectMake(0, 0, 250, 44)];
[self.searchController setDisplaysSearchBarInNavigationBar:YES];
[self.searchController setActive:YES animated:YES];
[self.searchController.searchBar becomeFirstResponder];
}
-(void)cancelEditing
{
[self.navigationItem setRightBarButtonItem:nil];
[self.searchController setActive:NO animated:YES];
[self.searchController.searchBar removeFromSuperview];
}
Upvotes: 2
Views: 890
Reputation: 3275
OK I came up with a solution for both problems.
The gap was occurring because of [self.searchController setDisplaysSearchBarInNavigationBar:YES];
which expected the NavigationBar to be translucent, so I've worked around by just enabling translucency on btnSearchClicked
and deactivating it on cancel.
The problem with the button was resolved by deactivating setDisplaysSearchBarInNavigationBar
in cancelEditing
before accessing the setRightBarButtonItem
property. Maybe that the setDisplaysSearchBarInNavigationBar
-Option creates an new NavigationBar which doesn't respond to self.navigationItem.
Upvotes: 1