对于任意版本的键盘,在键盘右上角加一个“完了”按钮,用于关闭键盘
下面这段代码写在自定义的控件里
// 初期化時
setup() {
let toolbar:UIToolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: 30))
let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
// ボタンの色、文字を設定する
let doneBtn: UIBarButtonItem = UIBarButtonItem(title: "完成", style: .done, target: self, action: #selector(doneButtonAction))
doneBtn.tintColor = UIColor.init(hexcode: "#D62C2C")
toolbar.setItems([flexSpace, doneBtn], animated: false)
toolbar.sizeToFit()
self.inputAccessoryView = toolbar
}
// 「完了ボタン」を押すと時、キーボードを閉じる
@objc func doneButtonAction() {
self.resignFirstResponder()
}