Show keyboard automatically when loading UISearchController

I created a UISearchController in a table view controller. I move on to this tabular view controller using the push segment from another view controller. I want the keyboard to appear with the cursor in the search bar as soon as the table view controller is pressed.

I made the search controller active in the viewDidLoad method using

self.mySearchController.active = true 

It makes an active search controller, but it does not call the keyboard and does not place the cursor in the search bar. I also tried

 self.mySearchController.searchBar.becomeFirstResponder() 

This line does not seem to have any effect.

How can I open the keyboard automatically / programmatically? Below is a more detailed version of my code.

 class PickAddressViewController: UITableViewController, UISearchResultsUpdating { var searchText = "" var mySearchController = UISearchController() override func viewDidLoad() { super.viewDidLoad() self.mySearchController = ({ let controller = UISearchController(searchResultsController: nil) controller.searchResultsUpdater = self controller.dimsBackgroundDuringPresentation = false controller.searchBar.sizeToFit() controller.searchBar.text = self.searchText self.tableView.tableHeaderView = controller.searchBar return controller })() self.mySearchController.active = true self.mySearchController.searchBar.becomeFirstResponder() } 
+7
ios swift uisearchcontroller
source share
5 answers

BecomeFirstResponder is the way to go, but you must not do this in viewDidLoad. See the following discussion - Unable to set searchBar as firstResponder

+3
source share

I also tried the suggestions listed in the link mentioned by Nikita Leonov. I needed to add a class for UISearchControllerDelegate and UISearchBarDelegate, and then it worked. I don't u

 class PickAddressViewController: UITableViewController, UISearchControllerDelegate, UISearchBarDelegate, UISearchResultsUpdating { override func viewDidLoad() { super.viewDidLoad() self.mySearchController = ({ controller.searchBar.delegate = self })() self.mySearchController.active = true self.mySearchController.delegate = self } func didPresentSearchController(searchController: UISearchController) { self.mySearchController.searchBar.becomeFirstResponder() } … } 
+2
source share
 override func viewDidAppear(animated: Bool) { super.viewDidAppear(true) self.navigationItem.titleView = searchController!.searchBar dispatch_async(dispatch_get_main_queue(), { self.searchController?.active = true self.searchController!.searchBar.becomeFirstResponder() }) } 

and this code

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

make sure you give searchController?.delegate = self in viewDidLoad (). Tested on iOS 9. *

+2
source share

Besides what other users suggested, I also did the following and it worked:

searchController.definesPresentationContext = true

0
source share

Swift 3 solution in my case:

 override func viewDidLoad() { super.viewDidLoad() navigationItem.titleView = mySearchController.searchBar mySearchController.searchResultsUpdater = self mySearchController.delegate = self } override func viewDidAppear(_ animated: Bool) { DispatchQueue.main.async { self.mySearchController.isActive = true } } func presentSearchController(_ searchController: UISearchController) { mySearchController.searchBar.becomeFirstResponder() } 
0
source share

All Articles