簡體   English   中英

Navigation11中帶有范圍按鈕的iOS11 SearchController

[英]iOS11 SearchController in NavigationBar with Scope Buttons

在此處輸入圖片說明

在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

我能夠使用Artem的解決方案,

searchController.dimsBackgroundDuringPresentation = false

但隨后我遇到了一個問題,即無法再單擊背景以消除對搜索欄的關注。

為解決此問題,我創建了自己的調光器uiview(屏幕尺寸,不透明度為25%的黑色背景色),並將其添加到當前顯示的屏幕的視圖中,並聽了輕按。 當用戶點擊它時,我將其淡出並設置

searchController.active = false

在您的ViewController中,您需要添加此行

self.definesPresentationContext = YES;

這告訴UISearchController它需要使用的表示上下文,該上下文由您的ViewController定義。

檢查此鏈接以獲取更多詳細信息: http : //asciiwwdc.com/2014/sessions/228

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM