最近我学会了制作自定义应用内键盘。现在我希望能够在多个自定义键盘之间进行切换。但是,重置textField.inputView
属性似乎不起作用。
我在以下项目中重新创建了这个问题的简化版本。s 代表实际的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
}
}
运行它给出了我最初期望的结果:弹出一个蓝色输入视图。
但是当我点击按钮切换到黄色输入视图时,什么也没有发生。为什么?我需要做什么才能使其正常工作?