Show keyboard automatically when UISearchController is loaded

后端 未结 6 1193
走了就别回头了
走了就别回头了 2021-02-05 23:39

I created a UISearchController in a table view controller. I segue to this table view controller using a push segue from another view controller. I want the keyboard to show up

相关标签:
6条回答
  • 2021-02-06 00:08
    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.* device

    0 讨论(0)
  • 2021-02-06 00:08

    Swift 5

    1. in viewDidLoad:
    searchViewController.delegate = self
    
    1. in viewDidAppear:
    searchViewController.isActive = true
    

    This activates the SearchController

    1. Define a delegate method:
    extension MyViewController: UISearchControllerDelegate {
        func didPresentSearchController(_ searchController: UISearchController) {
            DispatchQueue.main.async {
                searchController.searchBar.becomeFirstResponder()
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-06 00:12

    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 讨论(0)
  • 2021-02-06 00:17

    I also tried the suggestions listed in the link mentioned by Nikita Leonov. I needed to add make the class a UISearchControllerDelegate & 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()
        }
        …
    }
    
    0 讨论(0)
  • 2021-02-06 00:19

    BecomeFirstResponder is the way to go, but you should do it not in viewDidLoad. Look at following discussion for details - Cannot set searchBar as firstResponder

    0 讨论(0)
  • 2021-02-06 00:21

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

    searchController.definesPresentationContext = true

    0 讨论(0)
提交回复
热议问题