是否有可能在视图出现后立即自动打开键盘?
Is it possible that the keyboard opens up automatically as soon as the view appears?
我有两个视图控制器,第一个 IC 有一个按钮。
当我按下按钮时,第二个 IC 会按下视图。
第二个IC有一个搜索栏。
问题:
是否可以在将第二个界面控制器推入视图后立即自动打开键盘
这是我项目的 link:https://www.dropbox.com/sh/5jqwv9vfrtlbh9f/AACOAWy-OAqFTJGgDCYsy-xra?dl=0
这应该适用于包含搜索栏的视图控制器:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.searchController.searchBar.becomeFirstResponder()
}
编辑为 Swift
编辑 2:
在查看您的项目时,您可以通过在 viewDidAppear()
函数中调用 searchBar.canBecomeFirstResponder()
来确定 searchBar 尚未准备好成为第一响应者。我还没有深入了解 searchBar 何时能够成为第一响应者,但你可以使用 NSTimer 解决这个问题:
func showKeyboard() {
self.searchController.searchBar.becomeFirstResponder()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
var timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: Selector("showKeyboard"), userInfo: nil, repeats: false)
}
我有两个视图控制器,第一个 IC 有一个按钮。 当我按下按钮时,第二个 IC 会按下视图。 第二个IC有一个搜索栏。
问题:
是否可以在将第二个界面控制器推入视图后立即自动打开键盘
这是我项目的 link:https://www.dropbox.com/sh/5jqwv9vfrtlbh9f/AACOAWy-OAqFTJGgDCYsy-xra?dl=0
这应该适用于包含搜索栏的视图控制器:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.searchController.searchBar.becomeFirstResponder()
}
编辑为 Swift
编辑 2:
在查看您的项目时,您可以通过在 viewDidAppear()
函数中调用 searchBar.canBecomeFirstResponder()
来确定 searchBar 尚未准备好成为第一响应者。我还没有深入了解 searchBar 何时能够成为第一响应者,但你可以使用 NSTimer 解决这个问题:
func showKeyboard() {
self.searchController.searchBar.becomeFirstResponder()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
var timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: Selector("showKeyboard"), userInfo: nil, repeats: false)
}