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.

Leave a Comment