0
votes

I have a UISearchBar with some customizations and I create a UISearchDisplayController like this

self.searchController = [[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self];

I want the search bar to appear on navigation bar so I also set

self.searchDisplayController.displaysSearchBarInNavigationBar = true;

Now the search bar shows in the Navigation Bar, but I want to show the search bar of my UISearchDisplayController only when I tap on a Navigation Bar Button Item. I want to have a behaviour like:

  1. Hide the search bar initially
  2. Show search bar when a navigation bar button is clicked
  3. Hide the search bar when I tap "Cancel" button of the search bar

I tried to hide/unhide it like:

self.searchDisplayController.searchBar.hidden = YES;

but the code doesn't seem working. I've spent a lot time searching the solution to have the behaviour I want and still no luck. Thanks.

2
You've got three tasks lined up to complete. Are any of these working? - andrewbuilder
None of those steps is working as I haven't found out a way to hide/undide UISearchDisplayController's search bar. - Phyo Min Thu
Would you be content with having the search bar scrolled off screen (not animated) and beneath the top Nav bar, tapping the Nav bar retracts it to reveal search bar beneath (animated), click cancel on search bar reinstates Nav bar back over top of search bar (animated)? - andrewbuilder

2 Answers

0
votes

Try this out:

CGRect searchFrame = self.searchDisplayController.searchBar.frame;
searchFrame.size.height = 0;
self.searchDisplayController.searchBar.frame = searchFrame;
self.searchDisplayController.searchBar.hidden = YES;

EDIT: I just tried with below code and it worked. See if this helps you!

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.searchDisplayController.searchContentsController.navigationController setNavigationBarHidden:YES animated:YES];
    [self performSelector:@selector(test) withObject:nil afterDelay:2.0];
}


- (void)test {
    [self.searchDisplayController.searchContentsController.navigationController setNavigationBarHidden:NO animated:YES];
}
0
votes

Maybe this is a suitable solution?

To complete task 1, in your TVC lifecycle method viewDidLoad, insert a non-animated scroll that places the search bar beneath the Nav bar...

- (void)viewDidLoad {
    [super viewDidLoad];

    // Scroll off screen the search bar (44 points)
    [[self tableView] setContentOffset:CGPointMake(0, 44)];

    // other code for method
} 

To complete task 2, in your TVC create a custom action method associated with a UIBarButtonItem, that effectively hides the Nav bar, at the same time revealing the search bar...

- (IBAction)hideNavBar:(UIBarButtonItem *)sender {

    [[self navigationController] setNavigationBarHidden:YES animated:YES]

} 

To complete task 3, in your TVC use the UISearchDisplayController Delegate method to effectively display the Nav bar and at the same time hide the search bar...

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {

    [[self navigationController] setNavigationBarHidden:NO animated:YES];
    [[self tableView] setContentOffset:CGPointMake(0, 44)];

    // You might also like to...
    [self setSearchBarText:nil]; // if you are using a property to hold the search bar text
    [self setSearchResults:nil]; // if you are using a property to hold the search results

}