您可以将自己的视图控制器分配给 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.
}
}
我使用带有名称的 xib 文件CustomInputViewController.xib
来设计键盘