带有范围按钮的导航栏中的 iOS 11 搜索 Controller

标签 ios swift ios11

enter image description here

在 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
}

在我的委托(delegate)中,搜索会正确触发和过滤。但是,如果我单击任一范围按钮,它们就会消失。永远不会调用此委托(delegate)方法。 (按范围过滤还没有真正实现)

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

关于带有范围按钮的导航栏中的 iOS 11 搜索 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45683725/

相关文章:

ios - 如何在Scenekit场景编辑器中设置漫反射贴图

ios - 如何更新已呈现的上下文菜单中的值?

ios - 键盘上方的工具栏未显示在 -- iOS 11 上

ios - UIImagePickerController 返回 H.265 而不是 H.264

IOS - 我想回收Viewcontroller上的服务器数据

ios - 检测旋转的 UIView 之间的碰撞

ios - 如何从屏幕上删除行?

ios - 尝试在 Swift 中初始化 UIActionSheet 时出现 EXC_BAD_ACCESS

ios - Swift 将计算出的数据传递回之前的 View Controller

ios - 从 AVPlayerViewController 中删除 Quicktime Logo