加载 UISearchController 时自动显示键盘
Show keyboard automatically when UISearchController is loaded
我在 table 视图控制器中创建了一个 UISearchController。我使用来自另一个视图控制器的推送 segue 转至此 table 视图控制器。我希望键盘在按下 table 视图控制器后立即在搜索栏中显示光标。
我使用
在 viewDidLoad 方法中激活了搜索控制器
self.mySearchController.active = true
它确实使搜索控制器处于活动状态,但这不会调出键盘,也不会将光标放在搜索栏中。我也试过了
self.mySearchController.searchBar.becomeFirstResponder()
这一行好像没有任何作用。
如何调出键盘 automatically/programmatically?下面是我的代码的更详细版本
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 中执行此操作。查看以下讨论以了解详细信息 -
我也尝试了 Nikita Leonov 提到的 link 中列出的建议。我需要添加 make the class a UISearchControllerDelegate & UISearchBarDelegate 然后它起作用了。我没有你
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()
}
…
}
除了按照其他用户的建议进行操作外,我还进行了以下操作,并且奏效了:
searchController.definesPresentationContext = true
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()
})
}
和这段代码
func presentSearchController(searchController: UISearchController) {
searchController.searchBar.becomeFirstResponder()
}
确保你给
searchController?.delegate = self
在 viewDidLoad() 中。在 iOS 9.* 设备
上测试
Swift 3 在我的案例中的解决方案:
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()
}
Swift 5
- 在
viewDidLoad
中:
searchViewController.delegate = self
- 在
viewDidAppear
中:
searchViewController.isActive = true
这将激活 SearchController
- 定义委托方法:
extension MyViewController: UISearchControllerDelegate {
func didPresentSearchController(_ searchController: UISearchController) {
DispatchQueue.main.async {
searchController.searchBar.becomeFirstResponder()
}
}
}
我在 table 视图控制器中创建了一个 UISearchController。我使用来自另一个视图控制器的推送 segue 转至此 table 视图控制器。我希望键盘在按下 table 视图控制器后立即在搜索栏中显示光标。
我使用
在 viewDidLoad 方法中激活了搜索控制器self.mySearchController.active = true
它确实使搜索控制器处于活动状态,但这不会调出键盘,也不会将光标放在搜索栏中。我也试过了
self.mySearchController.searchBar.becomeFirstResponder()
这一行好像没有任何作用。
如何调出键盘 automatically/programmatically?下面是我的代码的更详细版本
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 中执行此操作。查看以下讨论以了解详细信息 -
我也尝试了 Nikita Leonov 提到的 link 中列出的建议。我需要添加 make the class a UISearchControllerDelegate & UISearchBarDelegate 然后它起作用了。我没有你
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()
}
…
}
除了按照其他用户的建议进行操作外,我还进行了以下操作,并且奏效了:
searchController.definesPresentationContext = true
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()
})
}
和这段代码
func presentSearchController(searchController: UISearchController) {
searchController.searchBar.becomeFirstResponder()
}
确保你给
searchController?.delegate = self
在 viewDidLoad() 中。在 iOS 9.* 设备
Swift 3 在我的案例中的解决方案:
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()
}
Swift 5
- 在
viewDidLoad
中:
searchViewController.delegate = self
- 在
viewDidAppear
中:
searchViewController.isActive = true
这将激活 SearchController
- 定义委托方法:
extension MyViewController: UISearchControllerDelegate {
func didPresentSearchController(_ searchController: UISearchController) {
DispatchQueue.main.async {
searchController.searchBar.becomeFirstResponder()
}
}
}