Search code examples
iosswiftuisearchcontroller

How to auto enable Search bar in UISearchController when that page appear?


This is code about my search in the app.

    self.searchController = UISearchController(searchResultsController:  nil)
    self.searchController.searchResultsUpdater = self
    self.searchController.delegate = self
    self.searchController.searchBar.delegate = self
    self.searchController.hidesNavigationBarDuringPresentation = false
    self.searchController.dimsBackgroundDuringPresentation = true
    self.searchController.searchBar.tintColor = UIColor.gray
    self.navigationItem.titleView = searchController.searchBar
    self.definesPresentationContext = true

I want to my search bar auto active when the page appear?

Dose anyone has ideas to do that? You can look on my image to make clear.

enter image description here


Solution

  • Try to add to viewWillAppear self.searchController.searchBar.becomeFirstResponder()