搜索栏未显示在导航栏iOS 11中

时间:2018-07-04 15:53:58

标签: ios swift uinavigationbar ios11 uisearchcontroller

我将viewController推到我想要一个searchBar的位置,但是搜索栏根本没有显示。下面是代码。我想念什么吗?

var searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search here..."
definesPresentationContext = true
searchController.searchBar.delegate = self
searchController.searchBar.sizeToFit()
if #available(iOS 11.0, *) {

    self.navigationItem.searchController = searchController
} else {
    // Fallback on earlier versions
    navigationItem.titleView = searchController.searchBar
    navigationItem.titleView?.layoutSubviews()
}

2 个答案:

答案 0 :(得分:2)

您需要将此行添加到您的代码中:

navigationItem.hidesSearchBarWhenScrolling = false

这会删除滚动时隐藏的searchBar,并在按下视图控制器时显示它。

答案 1 :(得分:0)

因此,navigationItem.hidesSearchWhenScrolling仅在将searchController的{​​{1}}属性设置为navigationItem时有效。