Ios 在处于非活动状态时从UINavigationBar中删除UISearchController

Ios 在处于非活动状态时从UINavigationBar中删除UISearchController,ios,swift,Ios,Swift,当按下UINavigationBar上的按钮时,我试图添加一个UISearchController。然后,当再次按下按钮时,它将关闭搜索栏。到目前为止,我已经: @IBAction func showSearchBar(sender: UIBarButtonItem) { if searchController == nil { var resultsController = SearchResultsViewController() resultsCon

当按下
UINavigationBar
上的按钮时,我试图添加一个
UISearchController
。然后,当再次按下按钮时,它将关闭搜索栏。到目前为止,我已经:

@IBAction func showSearchBar(sender: UIBarButtonItem) {
    if searchController == nil {
        var resultsController = SearchResultsViewController()
        resultsController.searchResultsDelegate = self

        searchController = UISearchController(searchResultsController: resultsController)
        searchController!.searchResultsUpdater = resultsController

        searchController!.searchBar.frame = CGRect(
            x: searchController!.searchBar.frame.origin.x,
            y: searchController!.searchBar.frame.origin.y,
            width: searchController!.searchBar.frame.size.width,
            height: 44.0)
        searchController!.searchBar.center.x += self.view.bounds.width
    }

    if !searchBarActive {
        UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
            self.searchController!.searchBar.center.x -= self.view.bounds.width
        }, completion: nil)
        searchController?.hidesNavigationBarDuringPresentation = false
        searchController?.searchBar.searchBarStyle = UISearchBarStyle.Default
        searchController?.dimsBackgroundDuringPresentation = true
        navigationItem.titleView = searchController?.searchBar
        self.definesPresentationContext = true

    } else {
        UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
            self.searchController!.searchBar.center.x += self.view.bounds.width
        }, completion: nil)
        dismissViewControllerAnimated(true, completion: nil) // edited: tried this per comments but also does not work
        searchController!.active = false
    }

    searchBarActive = !searchBarActive
}

这会添加
searchController.searchBar
,并将其从屏幕右侧滑入。再次按下按钮时,它会将搜索栏滑回右侧。但是,搜索栏中覆盖的“我的后退”按钮无法重置以说出这些单词“返回,当我将
搜索栏滑出屏幕时,原来的
导航项
。标题也不会返回。”。我觉得我错过了一些东西,以便排除
searchController
,但我不知道正确的调用是什么。
searchController
.active属性似乎不起作用。

调用
dismissViewControllerAnimated:
关闭
UISearchController
并在方法的完成块中执行所有其他更改