18

UISearchDisplayController ではなく UISearchController を使用しており、すぐに SearchBar Tap に SearchResultController を表示したいと考えています。現在、次のように表示されています(検索バーをタップすると):

4

4 に答える 4

26

そのため、 を使用する方法をいじる必要があります。UISearchControllerviewControllerUISearchControllerDelegatewillPresentSearchController:

初期化後self.searchController、ViewController を UISearchControllerDelegate に準拠させます。

self.searchController.delegate = self;

ViewController に実装willPresentSearchController:します。

- (void)willPresentSearchController:(UISearchController *)searchController
{
    dispatch_async(dispatch_get_main_queue(), ^{
        searchController.searchResultsController.view.hidden = NO;
    });
}

そうしないと、内部動作によってオーバーライドされるため、非同期ディスパッチが必要です。ここでは、アニメーションを使用してテーブル ビューをフェードインさせることができます。

didPresentSearchController:さらに、健全性のために実装します。

- (void)didPresentSearchController:(UISearchController *)searchController
{
    searchController.searchResultsController.view.hidden = NO;
}
于 2015-03-22T17:36:19.927 に答える
5

Chris Vasselli の答えは、これを実装する最もクリーンな方法です。

これがSwift 3にあります

override func viewDidLoad() {
    super.viewDidLoad()

    searchController.delegate = self
    self.searchController.searchResultsController?.view.addObserver(self, forKeyPath: "hidden", options: [], context: nil)
}


override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {

    if let someView: UIView = object as! UIView? {

        if (someView == self.searchController.searchResultsController?.view &&
            (keyPath == "hidden") &&
            (searchController.searchResultsController?.view.isHidden)! &&
            searchController.searchBar.isFirstResponder) {

            searchController.searchResultsController?.view.isHidden = false
        }

    }
}


func willPresentSearchController(_ searchController: UISearchController) {
    searchController.searchResultsController?.view.isHidden = false
}


func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

    if (searchText.characters.count == 0) {
        searchController.searchResultsController?.view.isHidden = false
    }
}


func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
    searchController.searchResultsController?.view.isHidden = true
}
于 2016-09-30T05:52:11.877 に答える