How to hide search bar on scroll? (without UITableViewController)

Viewed 4604

I have a simple UIViewController containing a UITableView and I want to hide search bar inside of navigation bar on scrolling UITableView. I found

navigationItem.hidesSearchBarWhenScrolling = true

but it does not work. Is there any way to make it work on UIViewController?

3 Answers

SWIFT 4 simple implementation

Add UISearchBarDelegate delegate to your class

Then add this function in your class

func showSearchBar() {
    let searchController = UISearchController(searchResultsController: nil)
    searchController.searchBar.delegate = self
    searchController.dimsBackgroundDuringPresentation = false
    searchController.hidesNavigationBarDuringPresentation = true
    navigationItem.hidesSearchBarWhenScrolling = true 
    //true for hiding, false for keep showing while scrolling
    searchController.searchBar.sizeToFit()
    searchController.searchBar.returnKeyType = UIReturnKeyType.search
    searchController.searchBar.placeholder = "Search here"
    navigationItem.searchController = searchController
}

Then call the function in your viewDidLoad()

My guesses are when you first enter the UIViewController the UISearchBar is by default hidden, considering you're using UISearchController and access it with a search icon or something.

For this case, iOS by default doesn't hide the UISearchBar and (hides it/shows it) based on your scroll direction in the UITableView now how to solve this .

Since there are nothing we can do to cancel this behavior but we can have some work around slightly better when a user tapes cancel we set the UISearchController to nil to remove it from the UIViewController and when the user tap on the search icon, we gives it a value again and show it .

Note : This answer assuming that you implement it that way considering there is not much context nor code to in the question .

Lets get into the code .

 func prepareSearchController() { //function to setup the SearchBar  
    // Setup the Search Controller
    searchController.searchResultsUpdater = self
    searchController.searchBar.delegate = self
    searchController.obscuresBackgroundDuringPresentation = false
    searchController.searchBar.placeholder = "Search Items"
    navigationItem.searchController = searchController
    definesPresentationContext = true
}

   @objc func openSearch() { // when user tap on the search icon 
    prepareSearchController()
    self.navigationItem.searchController?.isActive = true
}

Now we conform to UISearchBarDelegate to access the cancel button action.

 func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { // this function is available only in search bar delegate  make sure to conform to it . 
    self.navigationItem.searchController = nil 
}

Please Try this approach

override func viewWillAppear(_ animated: Bool) {
        if #available(iOS 11.0, *) {
              navigationItem.hidesSearchBarWhenScrolling = false
       }
}

override func viewDidAppear(_ animated: Bool) {
        if #available(iOS 11.0, *) {
              navigationItem.hidesSearchBarWhenScrolling = true
      }
}
Related