Ios 如何在显示searchController时自动显示键盘

Ios 如何在显示searchController时自动显示键盘,ios,swift,button,uisearchbar,uisearchcontroller,Ios,Swift,Button,Uisearchbar,Uisearchcontroller,如何在显示searchController时自动显示键盘?我尝试了很多解决方案,但没有一个对我有效..包括成为第一响应者()等等。。。请帮助首先,移动: @IBAction func MapSearchController(_ sender: UIBarButtonItem) { let searchTable = storyboard!.instantiateViewController(withIdentifier: "SearchTableViewController") as!

如何在显示searchController时自动显示键盘?我尝试了很多解决方案,但没有一个对我有效..包括
成为第一响应者()
等等。。。请帮助

首先,移动:

@IBAction func MapSearchController(_ sender: UIBarButtonItem) {

    let searchTable = storyboard!.instantiateViewController(withIdentifier: "SearchTableViewController") as! SearchTableViewController

        mapSearchController = UISearchController(searchResultsController: searchTable)
        mapSearchController.searchResultsUpdater = searchTable
        present(mapSearchController!, animated: true, completion: nil)
        mapSearchController.searchBar.becomeFirstResponder()
        self.mapSearchController.dimsBackgroundDuringPresentation = true
        self.mapSearchController.searchBar.sizeToFit()
        self.mapSearchController.searchBar.barTintColor = UIColor.black
        self.mapSearchController.searchBar.placeholder = "חפש ברים"
        self.mapSearchController.hidesNavigationBarDuringPresentation = true
        mapSearchController.searchBar.delegate = self
        definesPresentationContext = true
        searchTable.mapView = mapView
        searchTable.handleMapSearchDelegate = self
最后,完成mapSearchController上所有属性的设置后

然后确保添加委托方法:

present(mapSearchController!, animated: true, completion: nil)
如果仍然不起作用,请确保此VC符合搜索UISearchControllerDelegate。

首先,移动:

@IBAction func MapSearchController(_ sender: UIBarButtonItem) {

    let searchTable = storyboard!.instantiateViewController(withIdentifier: "SearchTableViewController") as! SearchTableViewController

        mapSearchController = UISearchController(searchResultsController: searchTable)
        mapSearchController.searchResultsUpdater = searchTable
        present(mapSearchController!, animated: true, completion: nil)
        mapSearchController.searchBar.becomeFirstResponder()
        self.mapSearchController.dimsBackgroundDuringPresentation = true
        self.mapSearchController.searchBar.sizeToFit()
        self.mapSearchController.searchBar.barTintColor = UIColor.black
        self.mapSearchController.searchBar.placeholder = "חפש ברים"
        self.mapSearchController.hidesNavigationBarDuringPresentation = true
        mapSearchController.searchBar.delegate = self
        definesPresentationContext = true
        searchTable.mapView = mapView
        searchTable.handleMapSearchDelegate = self
最后,完成mapSearchController上所有属性的设置后

然后确保添加委托方法:

present(mapSearchController!, animated: true, completion: nil)

如果仍然不起作用,请确保此VC符合搜索UISearchControllerDelegate。

您设置得太早了。此时,
MapSearchController
实例的
viewDidLoad
尚未触发。您只能在
viewDidLoad
之后成为第一响应者


因此,您需要做的是:在
MapSearchController
视图中设置
becomeFirstResponder
,您设置得太早了。此时,
MapSearchController
实例的
viewDidLoad
尚未触发。您只能在
viewDidLoad
之后成为第一响应者


因此,您需要做的是:在
MapSearchController

视图中设置
becomeFirstResponder
,问题是加载视图id时搜索控制器不显示,因此即使在设置becomeFirstResponder后也无法解决问题。下面的代码将修复该问题,并在搜索栏中用光标打开键盘

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

问题是加载视图id时搜索控制器不显示,所以即使在生成becomeFirstResponder后也无法解决问题。下面的代码将修复该问题,并在搜索栏中用光标打开键盘

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

将其设置为first ResponderReady所做的。。。对我不起作用…这里也有同样的问题。尝试了所有的解决方案,包括下面提到的解决方案。不工作。代码9,iOS11。在模拟器和设备上。将其设置为first ResponderReady所做的。。。对我不起作用…这里也有同样的问题。尝试了所有的解决方案,包括下面提到的解决方案。不工作。代码9,iOS11。在模拟器和设备上。它以什么方式不起作用?是否触发了didPresentSearchController?idk。。。我如何检查它是否触发了。。。在我的设备上,键盘不显示。。。我必须按下搜索栏才能显示upfirst move making应用程序崩溃,并说
致命错误:在打开可选值时意外发现nil
以何种方式不起作用?是否触发了didPresentSearchController?idk。。。我如何检查它是否触发了。。。在我的设备上,键盘不显示。。。我必须按下搜索栏才能显示upfirst move making应用程序崩溃,并说
致命错误:在打开可选值时意外发现nil