您可以将自己的视图控制器分配给 inputViewcontroller:
您的 viewController 必须是 UIInputViewController 的子类,例如:
class CustomInputViewController: UIInputViewController {
@IBOutlet var insertTextButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
self.inputView?.translatesAutoresizingMaskIntoConstraints = false
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func insertText(_ button: UIButton){
self.textDocumentProxy.insertText((button.titleLabel?.text)!);
}
}
这里只有一个按钮 insertTextButton 我在 xib 文件中设计。
在您的主视图控制器中,您需要一个文本字段(或文本视图)的子类:
class textfield: UITextField {
var _inputViewController : UIInputViewController?
override public var inputViewController: UIInputViewController?{
get { return _inputViewController }
set { _inputViewController = newValue }
}
}
你分配给你的文本字段。
现在您可以将自己的 inputViewcontroller 分配给您的文本字段,例如:
class ViewController: UIViewController {
private var customInputViewController = CustomInputViewController(nibName: "CustomInputViewController",
bundle: nil)
@IBOutlet var textField: textfield!
override func viewDidLoad() {
super.viewDidLoad()
self.textField.inputViewController = customInputViewController
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我使用名为 CustomInputViewController.xib 的 xib 文件来设计键盘