UISearchController: show results even when search bar is empty

前端 未结 14 2178
独厮守ぢ
独厮守ぢ 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:41

    What is being hidden is the search results controller's view. Therefore it is sufficient to unhide it any time it might be hidden. Simply do as follows in the search results controller:

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.view.isHidden = false
    }
    
    func updateSearchResults(for searchController: UISearchController) {
        self.view.isHidden = false
        // ... your other code goes here ...
    }
    

    Now the results view (i.e. the table view) is always visible, even when the search bar text is empty.

    By the way, the iOS Mail app behaves like this, and I assume that's how it's implemented (unless Apple has access to some secret private UISearchController setting).

    [Tested in iOS 10 and iOS 11; I didn't test on any earlier system.]

提交回复
热议问题