我正常设置带有数字键盘的文本字段以及解除键盘和resignFirstResponder操作,但是当我点击背景时,数字键盘会变为普通键盘,然后当我再次点击背景键盘时,键盘会自动关闭 - 陌生!如何只需点击一下即可关闭数字键盘?
self.textField.delegate = self
textField.keyboardType = UIKeyboardType.DecimalPad
@IBAction func tapBackground(sender: AnyObject) {
view.endEditing(true)
}
@IBAction func viewTapped(sender: AnyObject) {
textField.resignFirstResponder()
}
答案 0 :(得分:1)
尝试以下操作可能会对您有所帮助:)
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: "handleTapGesture:")
tap.delegate = self
self.view.addGestureRecognizer(tap)
}
// MARK: - UITapGestureRecognizer
func handleTapGesture(gesture : UITapGestureRecognizer) {
self.view.endEditing(true)
}
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool
{
return true
}