Rutger Huijsmans Rutger Huijsmans - 2 months ago 25
Swift Question

Activate UISearchBar

I'm trying to make a view load as if the users has already pressed into the UISearchBar. I want the searchController to be loaded with the UISearchBar on top along with the cancel button.

I've tried this:

func activateSearch(){
self.searchBarShouldBeginEditing(self.searchController.searchBar)
self.willPresentSearchController(self.searchController)
self.updateSearchResultsForSearchController(self.searchController)
}

Answer

to enable the seacrbar focus just add becomeFirstResponder to your search bar

 func activateSearch(){
 self.searchController.searchBar.becomeFirstResponder()
}

or if above is not help add self.searchController.active = true and try this

   func activateSearch(){
      self.searchController.active = true 
     self.searchController.searchBar.becomeFirstResponder()
}

or improved way

  func activateSearch(){
self.searchController.active = true 
self.searchController.searchBar.hidden = false

self.searchController.searchBar.becomeFirstResponder()
 }

no need of this

func activateSearch(){
    self.searchBarShouldBeginEditing(self.searchController.searchBar)
    self.willPresentSearchController(self.searchController)
    self.updateSearchResultsForSearchController(self.searchController)
}