简体   繁体   中英

UISearchController's UISearchBar embedded in Navigation Bar: Does not become first responder

I am trying to embed a UISearchController's search bar in the titleView of my navigation controller's bar. Exactly this can be seen in the UICatalog sample code by Apple and works:

// Create the search results view controller and use it for the UISearchController.
AAPLSearchResultsViewController *searchResultsController = [self.storyboard instantiateViewControllerWithIdentifier:AAPLSearchResultsViewControllerStoryboardIdentifier];

// Create the search controller and make it perform the results updating.
self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
self.searchController.searchResultsUpdater = searchResultsController;
self.searchController.hidesNavigationBarDuringPresentation = NO;

/*
    Configure the search controller's search bar. For more information on how to configure
    search bars, see the "Search Bar" group under "Search".
*/
self.searchController.searchBar.searchBarStyle = UISearchBarStyleMinimal;
self.searchController.searchBar.placeholder = NSLocalizedString(@"Search", nil);

// Include the search bar within the navigation bar.
self.navigationItem.titleView = self.searchController.searchBar;

self.definesPresentationContext = YES;

I try to do the same with this code:

 self.searchSuggestionsController = [[SearchSuggestionsTableViewController alloc]initWithStyle:UITableViewStylePlain];
self.searchSuggestionsController.delegate = self;
self.searchController = [[UISearchController alloc]initWithSearchResultsController:self.searchSuggestionsController];
self.searchController.delegate = self;
self.searchController.searchResultsUpdater =  self.searchSuggestionsController;
self.searchController.searchBar.delegate = self;

self.searchController.dimsBackgroundDuringPresentation = YES;
[self.searchController setHidesNavigationBarDuringPresentation:NO];



self.searchController.searchBar.searchBarStyle = UISearchBarStyleProminent;
[self.searchController.searchBar setPlaceholder:NSLocalizedString(@"Search", nil)];

[self.navigationItem setTitleView:self.searchController.searchBar];

if (self.searchQuery) {
    [self.searchController.searchBar setText:_searchQuery.queryString];
}

self.definesPresentationContext = YES;

But when I run it on device or in simulator the search bar in the navigation bar will never become first responder. The keyboard is not showing up, I can't enter text. What I can do is use the clear button and the text clears but still I can't enter any texts.

Any ideas?

try implementing UISearchBarDelegate

and call searchBar ' s becomeFirstResponder() in searchBarTextDidBeginEditing

then in searchBarTextDidEndEditing call resignFirstResponder()

Old question, but just had a similar problem. Be sure to have embedded the "searchBar" in a valid navigation controller in the VC heirachy. That happened to be my mistake. So this works;

CCCTestVC *tVC = [[CCCTestVC alloc] init];
UINavigationController *nC = [[UINavigationController alloc] initWithRootViewController:tVC];
[self.navigationController  presentViewController:nC animated:YES completion:nil];

as opposed to doing this;

CCCTestVC *tVC = [[CCCTestVC alloc] init];
[self.navigationController pushViewController:tVC animated:YES];

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