简体   繁体   中英

Can't edit UISearchController UISearchBar in navigationItem titleView

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).

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.

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

} 

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!!

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM