我已经呆了两天了,但我无法让UISearchController / NavigationItem正常运行(应该是(在启动时隐藏,向上滚动后显示,向下滚动后隐藏))< / strong>。它是如此简单,显然除了我之外的每个人都可以做到。这是我想要的样子:http://blog.eppz.eu/swiftui-search-bar-in-the-navigation-bar/
这真的很令人沮丧,我只想知道外面是否有人可以引导我进行操作或将我定向到可以下载并迁移到我的应用程序的模板文件。
现在是我的viewDidLoad,但是我也尝试了不同的UISearchController组合,但它并没有按照我想要的那样工作:
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
tableView.tableHeaderView = searchController.searchBar
searchController.searchResultsUpdater = self
searchController.hidesNavigationBarDuringPresentation = true
searchController.obscuresBackgroundDuringPresentation = true
definesPresentationContext = true
searchController.searchBar.sizeToFit()
searchController.searchBar.placeholder = "Search Candies"
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = true
}
答案 0 :(得分:1)
您要在tableHeaderView上添加searchBar。
这对我来说不太好:tableView.tableHeaderView = searchController.searchBar
删除该行并再次测试。
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
searchController.searchResultsUpdater = self
searchController.hidesNavigationBarDuringPresentation = true
searchController.obscuresBackgroundDuringPresentation = true
definesPresentationContext = true
searchController.searchBar.placeholder = "Search Candies"
navigationItem.hidesSearchBarWhenScrolling = true
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if navigationItem.searchController == nil {
navigationItem.searchController = searchController
}
}