UISearchController Requires an additional crane to become the first responder

I switched my apps on one screen from UISearchBarto UISearchController. This is a table controller. According to the design, I should not first save the search bar in the user interface if it is not activated (usually it is common practice to keep the search bar as a "tableHeaderView"). The problem was that I have a search button when the "search bar" is activated, which must be activated and become the first responder.

When you click on the cancel button, it should be removed from the user interface. However, when I click the Search Panel button on the navigation bar, it activates UISearchController, providing a dull background, but the keyboard does not appear. I need to click again on the search bar to bring the keyboard to the interface.

Here's the action of the search bar button:

@IBAction func onTapSearch(_ sender: AnyObject) {
    self.view.addSubview(searchController.searchBar)
    searchController.isActive = true
    searchController.becomeFirstResponder()
    isSearchActive = true
    self.navigationController?.setToolbarHidden(true, animated: false)
}

I am tuning UISearchControllerin my method viewDidLoad. Let me know if there is any part code that you want to see, but regular code. And I confirmed that I’m not calling anywhere resignFirstResponder().

+4
source share
1 answer

try it,

Just replace this line

searchController.becomeFirstResponder()

Moreover, below

searchController.searchBar.becomeFirstResponder()

Edit

func didPresentSearchController(_ searchController1: UISearchController) {
    searchController1.searchBar.becomeFirstResponder()
}

.

+5

Source: https://habr.com/ru/post/1668369/


All Articles