SearchResultsController出现在UINavigationBar上

时间:2018-07-22 14:34:52

标签: swift uinavigationitem uisearchcontroller uisearchresultscontroller

我想拥有一个单独的viewController作为searchResultsController,但是,我得到了如下所示的异常行为。

我正在这样设置searchController;

func setUpSearchControllerAttributes() {
    searchController = UISearchController(searchResultsController: searchResultsController)
    searchController.delegate = searchResultsController
    searchController.definesPresentationContext = true
    searchController.hidesNavigationBarDuringPresentation = false
    searchController.dimsBackgroundDuringPresentation = false
    searchController.searchBar.sizeToFit()
    searchController.searchBar.backgroundColor = .white
    searchController.searchBar.barTintColor = .white
    searchController.searchBar.placeholder = "Search"
    searchController.searchBar.searchBarStyle = .minimal
}

当我添加时:

navigationItem.titleView = searchController.searchBar

我得到:

this page

出于说明目的,我将searchResultsController视图的颜色设置为.green(具有较低的alpha值)。

如您所见,searchResultsController与navigationBar重叠。

现在设置时:

navigationItem.searchController = searchController

我在下面得到了期望的结果:

navigationItem.titleView = searchController.searchBar

对此有任何解释吗?显然,问题出在将searchResultsController SearchBar设置为navigationItem TextView。

1 个答案:

答案 0 :(得分:0)

我有;

searchController.definesPresentationContext = true

需要替换为;

self.definesPresentationContext = true