加载UISearchController时自动显示键盘 [英] Show keyboard automatically when UISearchController is loaded

查看:162
本文介绍了加载UISearchController时自动显示键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表视图控制器中创建了一个UISearchController.我使用另一个视图控制器的推式sese来选择此表视图控制器.我希望一旦按下表视图控制器,键盘就会在搜索栏中显示光标.

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 with the cursor in the search bar as soon as the table view controller is pushed.

我使用

self.mySearchController.active = true

它的确使搜索控制器处于活动状态,但这不会抬起键盘,也不会将光标放在搜索栏中.我也尝试过

It does make the search controller active but this does not bring up the keyboard nor is the cursor placed in the search bar. I also tried

self.mySearchController.searchBar.becomeFirstResponder()

此行似乎没有任何作用.

This line does not seem to have any effect.

如何自动/以编程方式调出键盘?下面是我的代码的更详细版本

How do I bring up 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()
}

推荐答案

BecomeFirstResponder是必经之路,但您不应在viewDidLoad中这样做.请查看以下讨论以了解详细信息-无法将searchBar设置为firstResponder

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

这篇关于加载UISearchController时自动显示键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆