ios - iOS11 SearchController in NavigationBar with Scope Buttons -


enter image description here

in ios 11 can put uisearchcontroller in navigationbar few lines of code.

i set 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 delegate, search fires , filters properly. however, if click either of scope buttons, disappear. delegate method never called. (filter scope not implemented yet)

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

full source on github:

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


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -