UISearchController: show results even when search bar is empty

前端 未结 14 2256
独厮守ぢ
独厮守ぢ 2020-12-23 13:27

As I understand, the default behaviour of UISearchController is:

  1. On tapping search bar, background is dimmed and \'cancel\' button is shown.
14条回答
  •  無奈伤痛
    2020-12-23 13:33

    You can simply implement the UISearchResultsUpdating protocol and set the results controller view to always show in updateSearchResultsForSearchController:

     func updateSearchResultsForSearchController(searchController: UISearchController) {
    
       // Always show the search result controller
       searchController.searchResultsController?.view.hidden = false
    
       // Update your search results data and reload data
       ..
    }
    

    This works because the method is called even when the search bar is activated, without any text.

提交回复
热议问题