最近I learned to make custom in-app keyboards .现在我希望能够在多个自定义键盘之间进行交换。但是,重置 textField.inputView
属性似乎不起作用。
我在以下项目中重新创建了此问题的简化版本。 UIView
代表实际的自定义键盘。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
let blueInputView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 300))
blueInputView.backgroundColor = UIColor.blueColor()
textField.inputView = blueInputView
textField.becomeFirstResponder()
}
@IBAction func changeInputViewButtonTapped(sender: UIButton) {
let yellowInputView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 300))
yellowInputView.backgroundColor = UIColor.yellowColor()
// this doesn't cause the view to switch
textField.inputView = yellowInputView
}
}
运行它给出了我最初期望的结果:弹出一个蓝色输入 View 。
但是当我点击按钮切换到黄色输入 View 时,没有任何反应。为什么?我需要做什么才能让它发挥作用?
最佳答案
经过更多试验后,我现在找到了解决方案。我需要让第一响应者辞职,然后重新设置。任何作为顶 View subview 的第一响应者都可以通过调用 endEditing
间接退出。
@IBAction func changeInputViewButtonTapped(sender: UIButton) {
let yellowInputView = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 300))
yellowInputView.backgroundColor = UIColor.yellowColor()
// first do this
self.view.endEditing(true)
// or this
//textField.resignFirstResponder()
textField.inputView = yellowInputView
textField.becomeFirstResponder()
}
关于ios - 如何切换自定义应用内键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34825201/