LoginSignup
5
6

More than 5 years have passed since last update.

UISearchBarのresultSearchControllerをSearchBarが空値の状態でも表示する

Posted at

こんにちは :octocat:

通常のUISearchBarの実装では、SearchBarがテキストを持たないとき、もとのビューが見え続けます。
ですが、SearchBarがテキストを持たないときでもアクティブになったタイミングでsearchResultsControllerを表示したいときがあるかと思います。
検索したところStackOverFlowにジャストな記事()がありましたので、学習記録がてら日本語版として書いておきます。

環境

  • macOS 10.12 sierra
  • Xcode 8.0 (8A218a)
  • Swift 3.0

解決法

  • UISearchBarを実装しているクラスで、さらにUISearchControllerDelegateプロトコルを取り入れます
resultSearchController.delegate = self
  • willPresentSearchControllerを実装します。
extension HogeViewController: UISearchControllerDelegate {
    func willPresentSearchController(_ searchController: UISearchController) {
        DispatchQueue.main.async {
            searchController.searchResultsController!.view.isHidden = false
        }
    }
}
  • didPresentSearchControllerを実装します。
func didPresentSearchController(_ searchController: UISearchController) {
    searchController.searchResultsController!.view.isHidden = false
}

詳しくは元記事もあわせてご覧ください :eyes:

5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6