简体   繁体   中英

Search controller with navigation bar

I have navigation controller which has .hidesBarOnSwipe = true . As well when I perform search my navigation controller goes up and hides (as it should by default).

But if I try search something and then press (x) to clean what I've already written my navigation bar overlaps search bar but the second is still active and I can write something and it performs search.

So the question is how I can find method that invoked when I tap (x) button on search bar.

And how can I make my navigation bar be fixed and hidden while my search bar is still active because if it is still active I can swipe down I got my navigation bar overlapping search bar again?

Thank you!

you can get this function of X pressed

 func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
    if searchText == "" {
        print("SearchBar text cleared!")
        // do something whatever you need
    }
}

or use

func textFieldShouldClear(textField: UITextField) -> Bool {

 // do something here
return true
}

Swift3

func textFieldShouldClear(_ textField: UITextField) -> Bool {
// do something here
return true
}

or use this

func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
if (searchBar.text == "") {
    //Clear stuff here
}
}

for more information you can get the samples from here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM