我在想要searchBar的地方按viewController,但是搜索栏根本不显示。下面是代码。我是不是遗漏了什么?
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()
}发布于 2018-10-22 16:54:54
您需要将下面这一行添加到代码中:
navigationItem.hidesSearchBarWhenScrolling = false这移除了滚动时隐藏的searchBar,并在推动视图控制器时显示它。
发布于 2019-06-05 06:26:45
因此,navigationItem.hidesSearchWhenScrolling仅在您设置navigationItem的searchController属性时才起作用,而不是在您将navigationItem.titleView设置为searchBar时起作用。
https://stackoverflow.com/questions/51177387
复制相似问题