自定义UI控件:弹出键盘添加“完了”按钮

对于任意版本的键盘,在键盘右上角加一个“完了”按钮,用于关闭键盘
下面这段代码写在自定义的控件里

// 初期化時
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()
}
发布了10 篇原创文章 · 获赞 0 · 访问量 230

猜你喜欢

转载自blog.csdn.net/weixin_42163902/article/details/104080819