Cannot set searchBar as firstResponder Cannot set searchBar as firstResponder ios ios

Cannot set searchBar as firstResponder


I noticed this issue too. What seems to happen is the call to becomeFirstResponder is done when the searchController is still 'loading'. If you comment out becomeFirstResponder you notice that there is no difference. So we need a way to call becomeFirstResponder after the searchController is 'done' loading.

When I looked at various delegate methods I noticed there is a delegate method:

- (void)didPresentSearchController:(UISearchController *)searchController

This method is called right after the searchController has been presented. Then I make the call to becomeFirstResponder:

- (void)didPresentSearchController:(UISearchController *)searchController{    [searchController.searchBar becomeFirstResponder];}

This fixes the problem. You will notice that when the searchController is loaded, the searchbar now has focus.


The solution with - (void)didPresentSearchController:(UISearchController *)searchController did not work, since this delegate method is called only when the user taps on the search bar...

However, this solution did work:

- (void) viewDidAppear:(BOOL)animated{    [super viewDidAppear:animated];    [self performSelector:@selector(showKeyboard) withObject:nil afterDelay:0.1];}- (void) showKeyboard{    [self.searchController.searchBar becomeFirstResponder];}

Swift 3

delay(0.1) { self.searchController.searchBar.becomeFirstResponder() }func delay(_ delay: Double, closure: @escaping ()->()) {    let when = DispatchTime.now() + delay    DispatchQueue.main.asyncAfter(deadline: when, execute: closure)}


Well, I found the solution that is actually perfectly working for me.

Don't call [self.searchController setActive:YES]; before calling [self.searchController.searchBar becomeFirstResponder];

What's better, don't call [self.searchController setActive:YES]; at all.

Call only [self.searchController.searchBar becomeFirstResponder]; and the keyboard just pops out as it should, without any delay.

It seems to be somewhat like a bug and a lot of people are confirming it. For example, check here: When assigning focus via becomeFirstResponder to UISearchController's UISearchBar, the keyboard does not appear