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

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


当前回答

以下是对Luda的回答的全面检查,并进行了以下更改:

附件视图会自动调整到应用程序框架的宽度 避免使用已弃用的常量UIBarButtonItemStyleBordered Done按钮被实例化为UIBarButtonSystemItemDone

目前“Done”按钮位于附件视图的中央。您可以通过删除相关一侧的空格将其定位在左侧或右侧。

我省略了“取消”按钮,因为默认键盘也没有这个按钮。如果你确实想要一个“取消”按钮,我建议你实例化它为一个UIBarButtonSystemItemCancel,并确保你没有丢弃文本字段中的原始值。在Luda的回答中实现的“取消”行为会用空字符串覆盖值,这可能不是你想要的。

- (void)viewDidLoad {
  [super viewDidLoad];
  float appWidth = CGRectGetWidth([UIScreen mainScreen].applicationFrame);
  UIToolbar *accessoryView = [[UIToolbar alloc]
                              initWithFrame:CGRectMake(0, 0, appWidth, 0.1 * appWidth)];
  UIBarButtonItem *space = [[UIBarButtonItem alloc]
                            initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                            target:nil
                            action:nil];
  UIBarButtonItem *done = [[UIBarButtonItem alloc]
                           initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                           target:self
                           action:@selector(selectDoneButton)];
  accessoryView.items = @[space, done, space];
  self.valueField.inputAccessoryView = accessoryView;
}

- (void)selectDoneButton {
  [self.valueField resignFirstResponder];
}

有关构建附件视图的更多信息,请参阅Apple关于数据输入自定义视图的文档。你可能会想要参考UIToolbar和UIBarButtonItem的参考页面。

其他回答

另一个解决方案。如果屏幕上有其他非数字填充文本字段,则完美。

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
    numberToolbar.barStyle = UIBarStyleBlackTranslucent;
    numberToolbar.items = @[[[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad)],
                         [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                         [[UIBarButtonItem alloc]initWithTitle:@"Apply" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)]];
    [numberToolbar sizeToFit];
    numberTextField.inputAccessoryView = numberToolbar;
}

-(void)cancelNumberPad{
    [numberTextField resignFirstResponder];
    numberTextField.text = @"";
}

-(void)doneWithNumberPad{
    NSString *numberFromTheKeyboard = numberTextField.text;
    [numberTextField 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)
       }

我所见过的技巧是创建一个与整个视图大小相同的自定义透明按钮,然后在其click方法中,让文本字段辞去first responder。因此,用户可以单击字段之外的任何地方来取消键盘。

我发现@user1258240的答案是非常简洁的,因为这并不像设置一个returnKeyType属性那么简单。

只是想贡献我自己的“可重用”方法:

func SetDoneToolbar(field:UITextField) {
    let doneToolbar:UIToolbar = UIToolbar()

    doneToolbar.items=[
        UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: self, action: nil),
        UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.plain, target: self, action: #selector(ViewController.dismissKeyboard))
    ]

    doneToolbar.sizeToFit()
    field.inputAccessoryView = doneToolbar
}

override func viewDidLoad() {
    super.viewDidLoad()

    SetDoneToolbar(field: UITextField_1)
    SetDoneToolbar(field: UITextField_2)
    SetDoneToolbar(field: UITextField_3)
    SetDoneToolbar(field: UITextField_N)
}

以下是改编自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()
}