Prevent UISearchDisplayController Navigation Bar Hiding

Whenever the user starts editing the search string of the UISearchDisplayController , the search controller becomes active and hides the view navigation bar, presenting the view of the search table. Is it possible to prevent the UISearchDisplayController hiding the navigation bar without overriding it?

+60
iphone uinavigationcontroller uisearchdisplaycontroller
May 11 '10 at 17:51
source share
15 answers

The new UISearchController class introduced with iOS 8 has the hidesNavigationBarDuringPresentation property, which you can set to false if you want the navigation bar to be visible (by default it will be hidden anyway).

+40
Oct 21 '14 at 9:49
source share

I just debugged a bit in the UISearchDisplayController and found that it was calling a private method in the UINavigationController to hide the navigation bar. This happens at -setActive: animated :. If you subclass UISearchDisplayController and overwrite this method with the following code, you may not hide that the navigationBar is hiding by pretending to be already hidden.

 - (void)setActive:(BOOL)visible animated:(BOOL)animated; { if(self.active == visible) return; [self.searchContentsController.navigationController setNavigationBarHidden:YES animated:NO]; [super setActive:visible animated:animated]; [self.searchContentsController.navigationController setNavigationBarHidden:NO animated:NO]; if (visible) { [self.searchBar becomeFirstResponder]; } else { [self.searchBar resignFirstResponder]; } } 

Let me know if this works for you. I also hope that this does not break in future versions of iOS ... Tested only on iOS 4.0.

+58
Jul 15 '10 at 16:00
source share

The simplest solution and the lack of hacks.

 @interface MySearchDisplayController : UISearchDisplayController @end @implementation MySearchDisplayController - (void)setActive:(BOOL)visible animated:(BOOL)animated { [super setActive: visible animated: animated]; [self.searchContentsController.navigationController setNavigationBarHidden: NO animated: NO]; } @end 
+52
Sep 21
source share

The above answers did not help me. My solution is to trick the UISearchDisplayController into believing that there was no UINavigationController.

In your view controller add this method

 - (UINavigationController *)navigationController { return nil; } 

I had no adverse side effects for me, despite the seemingly very bad idea ... If you need to get to the navigation controller, use [super navigationController] .

+28
May 2 '11 at 17:37
source share

Tried it differently, without subclassing UISearchDisplayController. In your UIViewController class, where you set the delegate for UISearchDisplayController, implement searchDisplayControllerDidBeginSearch: and add using

 [self.navigationController setNavigationBarHidden:NO animated:YES]; 

Did the trick for me, hope this helps.

+8
Oct 23 '10 at 17:26
source share

Since iOS 8.0, the same behavior can be achieved by setting the UISearchController self.searchController.hidesNavigationBarDuringPresentation to false.

The code in Swift is as follows:

 searchController.hidesNavigationBarDuringPresentation = false 
+8
Feb 04 '16 at 8:18
source share

I came across this while solving a slightly different problem. When using UISearchDisplayController, I want the search bar to be in the navigation bar (not below).

It’s not difficult to place the search bar in the navigation bar (see UISearchBar and UINavigationItem ). However, UISearchDisplayController assumes that the search bar is always below the navigation bar and (as discussed here) insists on hiding the navigation bar when entering the search, so everything looks awful. In addition, the UISearchDisplayController displays a search bar that is lighter than usual.

I have found a solution. The trick is to (counter-intuitively) undo the UISearchDisplayController from controlling any UISearchBar at all. If you use xibs, this means deleting the search string instance, or at least disconnecting the output. Then create your own UISearchBar:

 - (void)viewDidLoad { [super viewDidLoad]; UISearchBar *searchBar = [[[UISearchBar alloc] init] autorelease]; [searchBar sizeToFit]; // standard size searchBar.delegate = self; // Add search bar to navigation bar self.navigationItem.titleView = searchBar; } 

You will need to manually activate the search display controller when the user deletes the search bar (in -searchBarShouldBeginEditing: and manually drops the search bar when the user finishes the search (in -searchDisplayControllerWillEndSearch: .

 #pragma mark <UISearchBarDelegate> - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar { // Manually activate search mode // Use animated=NO so we'll be able to immediately un-hide it again [self.searchDisplayController setActive:YES animated:NO]; // Hand over control to UISearchDisplayController during the search searchBar.delegate = (id <UISearchBarDelegate>)self.searchDisplayController; return YES; } #pragma mark <UISearchDisplayDelegate> - (void) searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller { // Un-hide the navigation bar that UISearchDisplayController hid [self.navigationController setNavigationBarHidden:NO animated:NO]; } - (void) searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller { UISearchBar *searchBar = (UISearchBar *)self.navigationItem.titleView; // Manually resign search mode [searchBar resignFirstResponder]; // Take back control of the search bar searchBar.delegate = self; } 
+4
Dec 21 '11 at 23:13
source share

A really nice solution, but this is a crash of my application for iOS6. I had to make the following changes to make it work.

 @implementation ICSearchDisplayController - (void)setActive:(BOOL)visible animated:(BOOL)animated { if (visible == YES) { [super setActive:visible animated:animated]; [self.searchContentsController.navigationController setNavigationBarHidden:NO animated:NO]; } else { [super setActive:NO animated:NO]; } } 
+3
Dec 08 '12 at 17:52
source share

This seems to solve it for me. Tested as in iOS5 / 6.1. No visual problems that I could see.

 - (void)viewDidAppear { [super viewDidAppear]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillAppear:) name:UIKeyboardWillShowNotification object:nil]; } -(void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)keyboardWillAppear:(NSNotification *)notification { [self.navigationController setNavigationBarHidden:NO animated:NO]; } -(void)viewDidLayoutSubviews{ [self.navigationController setNavigationBarHidden:NO animated:NO]; } 
+3
Mar 12 '13 at 1:32
source share

iOS 7 is a little joking ... for me this works great:

 /** * Overwrite the `setActive:animated:` method to make sure the UINavigationBar * does not get hidden and the SearchBar does not add space for the statusbar height. * * @param visible `YES` to display the search interface if it is not already displayed; NO to hide the search interface if it is currently displayed. * @param animated `YES` to use animation for a change in visible state, otherwise NO. */ - (void)setActive:(BOOL)visible animated:(BOOL)animated { [[UIApplication sharedApplication] setStatusBarHidden:YES]; [self.searchContentsController.navigationController setNavigationBarHidden:YES animated:NO]; [super setActive:visible animated:animated]; [self.searchContentsController.navigationController setNavigationBarHidden:NO animated:NO]; [[UIApplication sharedApplication] setStatusBarHidden:NO]; } 

Reason for showing / hiding the status bar

+3
Mar 27 '14 at 10:09
source share

I think the best solution is to implement the UISearchDisplayController yourself.

It is not that difficult. You just need to implement the UISearchBarDelegate for your UIViewController and enable the UITableView to display the search results.

+1
Aug 23 2018-11-11T00:
source share

@Pavel works fine. However, I tried to get this in the UIPopoverController, and the text in the field is slightly pushed when the text field of the search string becomes the first responder, and it looks a little ugly, so I fixed it by calling the super method using animated set to NO .

0
Dec 08
source share

As jrc noted, “disconnecting the UISearchDisplayController from controlling any UISearchBar” seems to work for me. If I pass nil as a parameter when creating the UISearchDisplayController, the navigation bar remains visible at all times:

 searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:nil contentsController:self]; 
0
Apr 15 '13 at 8:34
source share

I added a custom navigation bar on my ViewController that was hiding during the search, a quick but not very good problem:

 - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{ [self.view addSubview:_navBar]; } 

_navBar is a UINavigationBar added programmatically, this helped me hide the navigation bar.

0
Feb 10 '15 at 6:08
source share

Just wanted to add an answer to stigi. When you cancel the search and start the search again, the table of search results will not respond to touches, so you need to add the following line

 self.searchResultsTableView.alpha = 1; 

Thus, the updated code is as follows

  - (void)setActive:(BOOL)visible animated:(BOOL)animated; { if(self.active == visible) return; if (visible) { [self.searchContentsController.navigationController setNavigationBarHidden:YES animated:NO]; [super setActive:visible animated:animated]; [self.searchContentsController.navigationController setNavigationBarHidden:NO animated:NO]; self.searchResultsTableView.alpha = 1; [self.searchBar becomeFirstResponder]; } else { [super setActive:visible animated:animated]; [self.searchBar resignFirstResponder]; } } 
0
Jul 29 '15 at 10:53
source share



All Articles