数字键盘在 swift 后退为普通键盘

numeric keyboard resigns to normal key board in swift

我对带有数字键盘的文本字段进行了正常设置,并设置了关闭键盘和 resignFirstResponder 操作,但是当我点击背景时,数字键盘变为普通键盘,然后当我再次点击背景时,键盘关闭 - 奇怪!如何一键关闭数字键盘到背景?

self.textField.delegate = self
        textField.keyboardType = UIKeyboardType.DecimalPad

 @IBAction func tapBackground(sender: AnyObject) {
        view.endEditing(true)
    }

@IBAction func viewTapped(sender: AnyObject) {
       textField.resignFirstResponder()
    }

试试看这可能对你有帮助:)

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
 }