切换到另一个视图时隐藏 UISearchController

Hide UISearchController when segue to another view

我想在 segue 时从 tableHeaderView 中隐藏我的搜索栏。我怎样才能做到这一点?我按如下方式创建搜索栏:

override func viewDidLoad() {
   ...
   resultSearchController = ({
        let searchController = UISearchController(searchResultsController: nil)       //  The results of the search will be presented in the current Table View, so the searchResultsController parameter of the UISearchController init method is set to nil.

        searchController.delegate = self        
        searchController.searchBar.delegate = self          // Without this selectedScopeButtonIndexDidChange won't get called.
        searchController.searchResultsUpdater = self
        searchController.hidesNavigationBarDuringPresentation = true    // NOTE: if false and tapping on a reminder to go the its details and then back then the titles may be screwed up.
        searchController.dimsBackgroundDuringPresentation = false       // NOTE: If true it would result in the filtered list not being scrollable.
        searchController.hidesBottomBarWhenPushed = true
        searchController.searchBar.sizeToFit()
        self.definesPresentationContext = false    

        searchController.searchBar.scopeButtonTitles = SCOPEBAR_OPTIONS.descriptionArray
        tableView.tableHeaderView = searchController.searchBar
        return searchController
    })()

我试着像这样简单地将它设置为 nil,但没有成功:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    tableView.tableHeaderView = nil
    ...
}

要关闭搜索控制器,请使用以下方法

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
  searchController.active = NO;
}

viewDidLoad() 中,您可以为此目的添加下一行

definesPresentationContext = true

最后,对我有用 (Swift 4):

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
         print("prepare for segue")
         searchController.isActive = false
    }

viewDidLoad() 中的方法 definesPresentationContext 对我也没有用 :

    definesPresentationContext = true

也不:

    searchController.definesPresentationContext = true

也不:

    self.definesPresentationContext = true