IOS11 SearchController in NavigationBar with Scope buttons

enter image description here

In iOS 11, you can put a UISearchController in a NavigationBar with a few lines of code.

I installed everything in 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
}

In my delegate, the search fires and filters correctly. However, if I click on any of the sphere buttons, they simply disappear. This delegate method is never called. (filter by volume has not yet been implemented)

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

The full source is on Github:

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

+4
source share
3 answers

Try adding this code to setupNavBar ():

searchController.dimsBackgroundDuringPresentation = false
+1

,

searchController.dimsBackgroundDuringPresentation = false

, .

, dimmer uiview ( , 25%) . ,

searchController.active = false
0

ViewController

self.definesPresentationContext = YES;

UISearchController , , ViewController.

: http://asciiwwdc.com/2014/sessions/228

0

Source: https://habr.com/ru/post/1683713/


All Articles