4

When setting a UISearchController search bar in the navigationItem titleView, the search bar can't be edited.

In my viewDidLoad I am configuring a UISearchController.

self.searchViewController = [self.storyboard instantiateViewControllerWithIdentifier:NSStringFromClass([SearchViewController class])];
self.searchViewController.delegate = self;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.searchViewController];
self.searchController.delegate = self;
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.delegate = self;
self.navigationItem.titleView = self.searchController.searchBar;

I can't tap the search bar. The cursor does not appear and there is no user interaction.

Oddly, if I initialize the UISearchController locally without setting it to a property, then I can edit the search bar, just no delegate callbacks.

self.searchViewController = [self.storyboard instantiateViewControllerWithIdentifier:NSStringFromClass([SearchViewController class])];
self.searchViewController.delegate = self;
UISearchController *searchController = [[UISearchController alloc] initWithSearchResultsController:self.searchViewController];
searchController.delegate = self;
searchController.searchResultsUpdater = self;
searchController.searchBar.delegate = self;
self.navigationItem.titleView = searchController.searchBar;

Another interesting behavior is that the clear button works (if some text is set in the search bar while initializing).

2
  • I have the same problem, and none of the suggested answers work. did you manage to find a solution for that?
    – Silvia H
    Jan 30, 2017 at 19:55
  • @SilviaHisham yes what worked for me is in my answer below stackoverflow.com/a/36893699/4099344
    – kylesyoon
    Feb 1, 2017 at 14:40

5 Answers 5

9

I had the same issue.

Imagine you have FirstViewController and SecondViewController, and booth have a UISearchBar on the titleView.

To fix the problem I had this code to booth UIViewController's.

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    self.definesPresentationContext = true

}

override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)

    self.definesPresentationContext = false

} 
2
  • 1
    Thank you! your answer is the only one that worked for me.
    – Silvia H
    Feb 23, 2017 at 10:12
  • This! This is the only answer that works! Thank you!!!
    – Soberman
    Nov 5, 2020 at 21:24
7

I am setting self.definesPresentationContext = YES; in the view controller that presents the view controller in question.

This must be set to self.definesPresentationContext = NO; in viewWillAppear:.

Now the search bar in the presented view controller can be edited.

2
  • set self.definesPresentationContext = NO; in which ViewController exactly? the one that has the UISearchController or the one before it?
    – Silvia H
    Feb 1, 2017 at 17:49
  • 1
    @SilviaHisham You can look at my answer for clarification stackoverflow.com/a/42148381/976628 Feb 9, 2017 at 22:50
1

Set your search bar to navigation title view :

self.navigationItem.titleView = self.searchBarTop;

then just set this view either left/right button of Navigation Bar

UIBarButtonItem *searchBarItem = [[UIBarButtonItem alloc] initWithCustomView:searchBar];
self.navigationItem.rightBarButtonItem = searchBarItem;

I hope this will work for You!!

1

I have just solved a very similar problem in my app, and thought I'd share the solution in case the existing solutions don't fix it for you.

In my case I had a tab bar application, with my custom controllers in the tabs, embedded in the navigation controllers. The symptoms were exactly the same, the search bar showed in the title area of the navigation bar, but was not interactive.

I have discovered that the problem was that I used my custom subclass of the UITabBarController and I have overriden the viewWillAppear(animated:) method, but forgot to call super.viewWillAppear(animated:) in the implementation. The additional symptom was that when I switched the tabs, the search bar suddenly became interactive and everything worked fine, just the interaction on the initial tab was disabled.

I hope this helps someone.

0

In your first block of code, you're instantiating with a SearchViewController identifier. In the second, you're using HBSearchViewController. This suggests that there might be another difference in your code besides using / not using an outlet.

1
  • Sorry that's my mistake. I removed the prefix in the first code block, forgot to in the second.
    – kylesyoon
    Apr 25, 2016 at 21:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.