Cannot set searchBar as firstResponder

前端 未结 21 1880
别跟我提以往
别跟我提以往 2020-11-30 01:33

I have a searchBar I\'m setting in a tableviewcontroller. i\'ve referenced this similar question UISearchBar cannot become first responder after UITableView did re-appear bu

21条回答
  •  星月不相逢
    2020-11-30 02:01

    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.

提交回复
热议问题