Show keyboard automatically when UISearchController is loaded

后端 未结 6 1278
走了就别回头了
走了就别回头了 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

提交回复
热议问题