0

In viewDidLoad():

searchController = UISearchController(searchResultsController: nil)
let cancelButtonAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(cancelButtonAttributes, for: .normal)

searchController?.dimsBackgroundDuringPresentation = false
searchController?.searchResultsUpdater = self
searchController?.delegate = self
searchController?.searchBar.tintColor = UIColor.black
searchController?.searchBar.barTintColor = UIColor(white: 0.9, alpha: 0.9)
searchController?.searchBar.placeholder = NSLocalizedString("Search", comment: "")
searchController?.hidesNavigationBarDuringPresentation = false
self.navigationItem.titleView = self.searchController?.searchBar

searchController?.isActive = true
self.searchController?.becomeFirstResponder()
self.searchController?.searchBar.becomeFirstResponder()

Here is the delegate method:

extension SearchMembers :  UISearchControllerDelegate {
    func didPresentSearchController(searchController: UISearchController){
          self.searchController?.searchBar.becomeFirstResponder()
    }
}

In my test didPresentSearchController(searchController:) is never called.

I don't know why but whatever I do just doesn't work.

Any idea?

EDIT The only solution that works

      delay(0.6) {
         self.searchController?.searchBar.becomeFirstResponder()
        }

Which creates an unwanted lag

Utku Dalmaz
  • 9,780
  • 28
  • 90
  • 130
  • I believe you want this: https://stackoverflow.com/questions/27951965/cannot-set-searchbar-as-firstresponder – digitalHound Aug 18 '18 at 00:15
  • @digitalHound I tried all answers on this page but didn't work – Utku Dalmaz Aug 18 '18 at 00:16
  • Use textfield in place of searchbar – Anil Kumar Aug 18 '18 at 02:59
  • “just doesn't work.” unclear what that means. Work, to do what? What are you trying to do? – matt Aug 18 '18 at 13:11
  • @matt I tried all of those answers on that page https://stackoverflow.com/questions/27951965/cannot-set-searchbar-as-firstresponder if you know any better please let me know – Utku Dalmaz Aug 18 '18 at 13:15
  • I didn’t ask what you tried. I asked what your goal is in the first place. What are you trying to make happen in the app? – matt Aug 18 '18 at 13:26
  • @matt sorry I misread your comment. I have a tableview controller that shows search results and I put a search controller into navigationItem.titleView. I just want it to becomeFirstResponder and show keyboard on viewdidload – Utku Dalmaz Aug 18 '18 at 13:28
  • Say `self.searchController?.isActive = true`. – matt Aug 18 '18 at 13:32

1 Answers1

0

According to the documentation of didPresentSearchController:

This method is only called when the search controller is automatically presented. It is not called if you explicitly present the search controller.

If you want to present the search results interface explicitly, wrap your search controller in a UISearchContainerViewController object and present that object instead.

ielyamani
  • 17,807
  • 10
  • 55
  • 90