When assigning focus via becomeFirstResponder to UISearchController's UISearchBar, the keyboard does not appear When assigning focus via becomeFirstResponder to UISearchController's UISearchBar, the keyboard does not appear objective-c objective-c

When assigning focus via becomeFirstResponder to UISearchController's UISearchBar, the keyboard does not appear


Your code looks ok. What you are describing isn't normal behaviour. The first thing you can do is to create a new project with just the UISearchController functionality and see how it goes. You can edit your question with it so we'll have a better view.

There's a good example on how to implement UISearchController here: Sample-UISearchController

Adding:

-(void)viewDidAppear:(BOOL)animated{    [super viewDidAppear:animated];    [self.searchController.searchBar becomeFirstResponder];}

to MasterViewController_TableResults.m gave the expected results and the keyboard popped up on launch on an iPad & iPhone with iOS 8.3.

You can go over that project and see what you did differently,

Edit:

Apparently if [self.searchController setActive:YES] is called before becomeFirstResponder the keyboard won't show. I wonder if that's a bug or not.


Had the same annoying issue.You would think that by setting the SearchController as active would both present the the search controller and the keyboard. Unfortunately, it only does the first part.

My solution

  • in viewDidAppear make the Search Controller active:

    override func viewDidAppear(animated: Bool) {  super.viewDidAppear(animated)  resultSearchController.active = true }
  • once it is active, in didPresentSearchController make as first responder

    func didPresentSearchController(searchController: UISearchController) {  searchController.searchBar.becomeFirstResponder() }


Swift 3.0 (iOS 10) working solution:

    override func viewDidAppear(_ animated: Bool) {        super.viewDidAppear(animated)        searchController.isActive = true        DispatchQueue.main.async { [unowned self] in            self.searchController.searchBar.becomeFirstResponder()        }    }