Rutger Huijsmans
Rutger Huijsmans

Reputation: 2408

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

Upvotes: 1

Views: 1914

Answers (2)

Med Abida
Med Abida

Reputation: 1222

this can be done simply by setting the searchController to active in viewDidAppear

override func viewDidAppear(animated: Bool) {
        searchController.active = true
    }

and add this

func didPresentSearchController(_ searchController: UISearchController) {
    DispatchQueue.main.async {
        searchController.searchBar.becomeFirstResponder()
    }
}

Upvotes: 1

Anbu.Karthik
Anbu.Karthik

Reputation: 82759

func activateSearch() {
    self.searchController.isActive = true 
    self.searchController.searchBar.isHidden = false
    self.searchController.searchBar.becomeFirstResponder()
 }

Upvotes: 4

Related Questions