所以我已经尝试了所有的方法,试图在Swift的导航栏中找到一个搜索栏。但遗憾的是我还没让它起作用,只是.
对于那些不知道我在说什么的人,我想做这样的事情

注意导航栏中的搜索栏。下面是我目前正在使用的
self.searchDisplayController?.displaysSearchBarInNavigationBar = true我在我的viewDidLoad中弹出了它,然后当我加载我收到的应用程序时,只是一个空的导航栏.)(有什么想法吗?
发布于 2019-09-28 16:59:17
在2019年,您应该使用UISearchController。
override func viewDidLoad() {
    super.viewDidLoad()
    let searchController = UISearchController(searchResultsController: nil)
    searchController.searchResultsUpdater = self.viewModel
    searchController.obscuresBackgroundDuringPresentation = false
    searchController.searchBar.placeholder = "Search artists"
    self.navigationItem.searchController = searchController
    self.definesPresentationContext = true
}有些类应该符合UISearchResultsUpdating。我通常将它作为扩展添加到我的ViewModel中。
extension ArtistSearchViewModel: UISearchResultsUpdating {
func updateSearchResults(for searchController: UISearchController) {
    print("Searching with: " + (searchController.searchBar.text ?? ""))
    let searchText = (searchController.searchBar.text ?? "")
    self.currentSearchText = searchText
    search()
    }
}这会产生这样的东西:

https://stackoverflow.com/questions/26726520
复制相似问题