我将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()
}
答案 0 :(得分:2)
您需要将此行添加到您的代码中:
navigationItem.hidesSearchBarWhenScrolling = false
这会删除滚动时隐藏的searchBar,并在按下视图控制器时显示它。
答案 1 :(得分:0)
因此,navigationItem.hidesSearchWhenScrolling
仅在将searchController
的{{1}}属性设置为navigationItem
时有效。