在. numberpad键盘类型上没有“完成”按钮。当用户在文本字段中输入数字信息时,如何使数字pad消失?

我可以使用默认键盘来获得“完成”按钮,但用户必须切换到数字键才能输入数字。有没有办法在数字板上显示“完成”按钮?


当前回答

Swift 2.2 /我用了Dx_的答案。然而,我希望所有的键盘都有这个功能。所以在我的基类中,我把代码:

func addDoneButtonForTextFields(views: [UIView]) {
    for view in views {
        if let textField = view as? UITextField {
            let doneToolbar = UIToolbar(frame: CGRectMake(0, 0, self.view.bounds.size.width, 50))

            let flexSpace = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)
            let done = UIBarButtonItem(title: "Done", style: .Done, target: self, action: #selector(dismissKeyboard))

            var items = [UIBarButtonItem]()
            items.append(flexSpace)
            items.append(done)

            doneToolbar.items = items
            doneToolbar.sizeToFit()

            textField.inputAccessoryView = doneToolbar
        } else {
            addDoneButtonForTextFields(view.subviews)
        }
    }
}

func dismissKeyboard() {
    dismissKeyboardForTextFields(self.view.subviews)
}

func dismissKeyboardForTextFields(views: [UIView]) {
    for view in views {
        if let textField = view as? UITextField {
            textField.resignFirstResponder()
        } else {
            dismissKeyboardForTextFields(view.subviews)
        }
    }
}

然后只需调用addDoneButtonForTextFields在self.view.subviews在viewDidLoad(或willDisplayCell如果使用表格视图)添加完成按钮到所有键盘。

其他回答

Swift 2.2 /我用了Dx_的答案。然而,我希望所有的键盘都有这个功能。所以在我的基类中,我把代码:

func addDoneButtonForTextFields(views: [UIView]) {
    for view in views {
        if let textField = view as? UITextField {
            let doneToolbar = UIToolbar(frame: CGRectMake(0, 0, self.view.bounds.size.width, 50))

            let flexSpace = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)
            let done = UIBarButtonItem(title: "Done", style: .Done, target: self, action: #selector(dismissKeyboard))

            var items = [UIBarButtonItem]()
            items.append(flexSpace)
            items.append(done)

            doneToolbar.items = items
            doneToolbar.sizeToFit()

            textField.inputAccessoryView = doneToolbar
        } else {
            addDoneButtonForTextFields(view.subviews)
        }
    }
}

func dismissKeyboard() {
    dismissKeyboardForTextFields(self.view.subviews)
}

func dismissKeyboardForTextFields(views: [UIView]) {
    for view in views {
        if let textField = view as? UITextField {
            textField.resignFirstResponder()
        } else {
            dismissKeyboardForTextFields(view.subviews)
        }
    }
}

然后只需调用addDoneButtonForTextFields在self.view.subviews在viewDidLoad(或willDisplayCell如果使用表格视图)添加完成按钮到所有键盘。

对于Swift 2.2,我使用这个

func addDoneButtonOnKeyboard() {
    let doneToolbar: UIToolbar = UIToolbar(frame: CGRectMake(0, 0, self.view.bounds.size.width, 50))

    let flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
    let done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: self, action: #selector(DetailViewController.finishDecimalKeypad))

    var items: [UIBarButtonItem]? = [UIBarButtonItem]()
    items?.append(flexSpace)
    items?.append(done)

    doneToolbar.items = items
    doneToolbar.sizeToFit()
    self.productPrice.inputAccessoryView=doneToolbar
}

func finishDecimalKeypad() {
    self.productPrice?.resignFirstResponder()
}
 let pickerView = UIPickerView()
   
 var yearpickerToolbar: UIToolbar?
   
    func createPickerView() {
          
           pickerView.delegate = self
           textfield.inputView = pickerView }
   
   
     func dismissPickerView() {
           let toolBar = UIToolbar()
           toolBar.sizeToFit()
   
           toolBar.isUserInteractionEnabled = true
           textfield.inputAccessoryView = toolBar
           textfield.delegate = self
   
   
   yearpickerToolbar = UIToolbar()
           yearpickerToolbar?.autoresizingMask = .flexibleHeight
   
           //add buttons
           let cancelButton = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action:#selector(cancelBtnClicked(_:)))
           cancelButton.tintColor = UIColor.blue
           let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
           
           let doneButton = UIBarButtonItem(title: "Done", style: .plain , target: self, action: #selector(self.doneBtnClicked(_ :) ))
           doneButton.tintColor = UIColor.blue
   
           doneButton.setTitleTextAttributes([NSAttributedString.Key.foregroundColor:
   UIColor.black], for: .normal)
           cancelButton.setTitleTextAttributes([NSAttributedString.Key.foregroundColor:
   UIColor.black], for: .normal)
           
           yearpickerToolbar?.items = [cancelButton, flexSpace, doneButton]
           textfield.inputAccessoryView = yearpickerToolbar }
   
   @objc func cancelBtnClicked(_ button: UIBarButtonItem?) {
           self.view.endEditing(true)
           
       }
   
       @objc func doneBtnClicked(_ button: UIBarButtonItem?) {
           self.view.endEditing(true)
       }

这是我遇到的最简单的解决办法。我是从《iOS 5开发入门》这本书中学到的。

假设数字字段名为numberField。

In ViewController, add the following method: -(IBAction)closeKeyboard:(id)sender; In ViewController.m, add the following code: -(IBAction)closeKeyboard:(id)sender { [numberField resignFirstResponder]; } Go back to nib file. Open Utilities pan. Open the Identity inspector under Utilities pan. Click on the View (in nib file) once. Make sure you have not clicked on any of the items in the view. For the sake of clarification, you should see UIView under Class in Identity inspector. Change the class from UIView to UIControl. Open Connection Inspector. Click and drag Touch Down and drop the arrow on File Owner icon. (FYI... File Owner icon is displayed on the left of View and appears as a hollow cube with yellow frame.) Select the method: closeKeyboard. Run the program.

现在,当你点击视图背景的任何地方,你应该能够解散键盘。

希望这能帮助你解决问题。: -)

以下是改编自Luda对斯威夫特的回答:

在你的UIViewController子类put的声明中

let numberToolbar: UIToolbar = UIToolbar()

在ViewDidLoad中放入:

    numberToolbar.barStyle = UIBarStyle.BlackTranslucent
    numberToolbar.items=[
        UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Bordered, target: self, action: "hoopla"),
        UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: self, action: nil),
        UIBarButtonItem(title: "Apply", style: UIBarButtonItemStyle.Bordered, target: self, action: "boopla")
    ]

    numberToolbar.sizeToFit()

    textField.inputAccessoryView = numberToolbar //do it for every relevant textfield if there are more than one 

并添加函数hoopla和hoopla(可以随意选择其他名称,只需相应地更改ViewDidLoad中的选择器名称

func boopla () {
    textField.resignFirstResponder()
}

func hoopla () {
    textField.text=""
    textField.resignFirstResponder()
}