带有范围按钮的NavigationBar中的iOS11 SearchController

《带有范围按钮的NavigationBar中的iOS11 SearchController》

在iOS 11中,您可以使用几行代码将UISearchController放在NavigationBar中.

我在ViewController.swift中设置了所有内容.

func setupNavBar() {
    navigationController?.navigationBar.prefersLargeTitles = true

    let searchController = UISearchController(searchResultsController: nil)
    searchController.searchResultsUpdater = wordViewController
    searchController.searchBar.scopeButtonTitles = ["French", "English"]
    searchController.searchBar.delegate = wordViewController

    navigationItem.searchController = searchController
    // Make searchbar persistent
    navigationItem.hidesSearchBarWhenScrolling = false
}

在我的代表中,搜索会正确触发和过滤.但是,如果我单击任一范围按钮,它们就会消失.永远不会调用此委托方法. (按范围过滤实际上尚未实现)

extension WordViewController: UISearchBarDelegate {

 func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {

    if let searchText = searchBar.text {
        print("Scoped changed: \(searchText)")
        filteredWordList = wordList.filter({$0.contains(searchText)})
    }
  }
}

完整资源来自Github:

https://github.com/melling/ios_topics/tree/master/NavBarSearch
https://github.com/melling/ios_topics/tree/master/NavBarSearch/NavBarSearch

最佳答案 尝试在setupNavBar()中添加此代码:

searchController.dimsBackgroundDuringPresentation = false
点赞