导航栏中的 UISearchController

UISearchController in navigationBar

我正在尝试在我的 tableViewController 中使用新的 UISearchController。

但是,当我像使用旧的 searchDisplayController 那样按下 searchBar 时,我对它如何在 navigationController 中向上移动感到有点困惑?

目前它只是停留在表头中。

这是我的代码:

    self.teamSearchController = ({
        let controller = UISearchController(searchResultsController: nil)

        controller.searchBar.searchBarStyle = UISearchBarStyle.Minimal
        controller.dimsBackgroundDuringPresentation = false
        controller.searchBar.sizeToFit()
       controller.searchBar.showsScopeBar = true
        self.tableView.tableHeaderView = controller.searchBar

        return controller
    })()

控制器:

当我点击搜索栏时:

您可以将 UISearchControllerUISearchBar 放在导航栏中,而不是 table header

self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.searchBar.searchBarStyle = UISearchBarStyleMinimal;

// Include the search bar within the navigation bar.
self.navigationItem.titleView = self.searchController.searchBar;

self.definesPresentationContext = YES;

Swift版本:

self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController.searchBar.searchBarStyle = UISearchBarStyle.Minimal

// Include the search bar within the navigation bar.
self.navigationItem.titleView = self.searchController.searchBar
self.definesPresentationContext = true

跟导航栏有关

func willPresentSearchController(searchController: UISearchController) {
    self.navigationController?.navigationBar.translucent = true
}

func willDismissSearchController(searchController: UISearchController) {
    self.navigationController?.navigationBar.translucent = false
}

如果您这样做,那么您将能够将其添加到 table 视图 header 并获得 baked-in 搜索控制器动画!