0

私はUINavigationControllerに埋め込まれたMKMapViewを提示するアプリケーションを持っています。 UINavigationControllerでは、私はUISearchControllerを配置しました。ユーザーがUISearchControllerをタッチすると、UITableViewControllerが表示されます。 UISearchControllerにScopeボタンを追加していない間はうまく動作します。UINavigationControllerに埋め込まれたUISearchControllerにスコープボタンを追加する方法

ここでは、アプリケーションを起動すると、UINavigationControllerのUISearchControllerのスクリーンショットが表示されます。私はUISearchControllerをタッチすると

enter image description here

次に、それはのUITableViewControllerとスコープ]ボタンが表示されます。

enter image description here

ここで彼らはUISearchControllerにうまく統合されていないので、我々はすでに次に、私が行くためにキャンセルボタンをタッチ

(色は半透明でなければなりません)スコープボタンで問題があります見ることができますバックメインのViewControllerには、UISearchControllerは元のスタイル回復していない

enter image description here

それは濃いグレーのボーダー(おそらく来ていますスコープボタンから)を選択します。ここで

は、私は私のメインのViewControllerのメインビューコントローラこの方法は、のviewDidLoadで呼び出され

func initSearchController() { 
    let mySearchController = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("SearchControllerId") as! SearchController 

    self.searchController = UISearchController(searchResultsController: mySearchController) 

    mySearchController.theSearchController = self.searchController 
    mySearchController.delegate = self 

    // Configure the UISearchController 
    self.searchController.searchResultsUpdater = self 
    self.searchController.delegate = self 

    self.searchController.searchBar.delegate = self 
    self.searchController.searchBar.placeholder = "data.." 
    self.searchController.hidesNavigationBarDuringPresentation = false 
    self.searchController.dimsBackgroundDuringPresentation = true 

    self.navigationItem.titleView = searchController.searchBar 

    self.definesPresentationContext = true 


} 

()でUISearchControllerを追加する方法です。

次に、SearchControllerが表示されたとき、私は私のTableViewControllerサブクラスに次のコードで

override func viewWillAppear(animated: Bool) { 
    super.viewWillAppear(animated) 

    // Mandatory to make sure the TableView is displayed when the search field is empty 
    // when user touch it. 
    view.hidden = false 

    var rect = delegate.searchController.searchBar.superview?.frame 
    rect?.size.height = 88 

    self.delegate.searchController.searchBar.scopeButtonTitles = ["one", "two", "three"] 
    self.delegate.searchController.searchBar.showsScopeBar = true 
    self.delegate.searchController.searchBar.superview?.frame = rect! 
} 

をスコープ]ボタンを追加していると検索が閉じているときに、次のコードが実行される

override func viewDidDisappear(animated: Bool) { 
    super.viewDidDisappear(animated) 


    var rect = delegate.searchController.searchBar.superview?.frame 
    rect?.size.height = 44 

    self.delegate.searchController.searchBar.superview?.frame = rect! 
    self.delegate.searchController.searchBar.showsScopeBar = false 
    self.delegate.searchController.searchBar.scopeButtonTitles = nil 
} 

ご覧のとおり、このコードでは重大な問題があります。

  1. スコープボタンが正しく表示されないと、私は素敵なアニメーション
  2. ユーザーはボタンが削除されている検索範囲を出るが、UISearchControllerのそれへの影響背景

でそれらを追加することができませんよUISearchControllerのスコープボタンを正しく統合するために、私が間違ってやっていることを教えてください。 私はUISearchControllerがUINavigationControllerに埋め込まれていない場合にのみ、例を見つけました。

ありがとうございました!

セバスチャン。

答えて

1

検索バーを使用してください。UISearchControllerのインスタンスでscopeButtonTitles:willAppear/didDisapearであなたのscopeButtonsを表示または非表示にする

func initSearchController() { 
    let mySearchController = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("SearchControllerId") as! SearchController 

    searchController = UISearchController(searchResultsController: mySearchController) 

    // Set Scope Bar Buttons 
    searchController.searchBar.scopeButtonTitles = ["one", "two", "three"] 
// searchController.searchBar.showsScopeBar = true //if you want it always visible 

    // Configure the UISearchController 
    searchController.searchResultsUpdater = self 
    searchController.searchBar.sizeToFit() 
    tableView.tableHeaderView = searchController.searchBar 

    searchController.delegate = self 
    searchController.searchBar.delegate = self 
    searchController.searchBar.placeholder = "data.." 
    searchController.hidesNavigationBarDuringPresentation = false 
    searchController.dimsBackgroundDuringPresentation = true 

    definesPresentationContext = true 
} 

必要はありません。これは、searchController.searchBar.showsScopeBar = trueで設定されます。

関連する問題