简体   繁体   English

编辑时在文本字段上隐藏键盘

[英]Hide Keyboard on Textfield when it Editing

How to hide a keyboard when I am using DateTime picker on UITextField . 当我在UITextField上使用DateTime选择器时如何隐藏键盘。 When I click on UITextField that time first show a keyboard than it shows DateTime Picker on selected UITextField . 当我点击UITextField ,首先显示一个键盘,而不是在所选的UITextField上显示DateTime Picker。 Please give me appropriate solution for this. 请给我适当的解决方案。

  1. Set the textField delegate to the object in which the textfield is initialise 将textField委托设置为初始化文本字段的对象

    textfield.delegate = self //object which will handle textfield delegate methods. textfield.delegate = self //将处理文本字段委托方法的对象。

  2. Implement the textfield delegate methods 实现textfield委托方法

     (BOOL)textFieldShouldReturn:(UITextField *)textField{ if ([textField canResignFirstResponder]) { [textField resignFirstResponder]; } return YES; } (BOOL)textFieldShouldEndEditing:(UITextField *)textField{ // add your method here return YES; } (void)textFieldDidEndEditing:(UITextField *)textField{ } 

In Swift 在斯威夫特

func textFieldDidEndEditing(textField: UITextField!) {    //delegate method

}

func textFieldShouldEndEditing(textField: UITextField!) -> Bool {  //delegate method
    return true
}

func textFieldShouldReturn(textField: UITextField!) -> Bool {   //delegate method
  textField.resignFirstResponder()

    return true
}

In the below delegate method of UITextField you can return false so that keypad won't come, and you can add the code to show your datepicker. 在下面的UITextField委托方法中,您可以返回false,这样键盘就不会出现,您可以添加代码来显示您的日期选择器。

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {

            //Your code to show datepicker

            return false
}

You can use Keyboard notifications to find find out wether Keyboard complete shown or not. 您可以使用Keyboard notifications来查找是否显示键盘完成。 When you find out keyboard is shown then show DateTime picker on your current textField . 当您发现键盘显示然后在当前textField上显示DateTime选择器。

Here is a code for Keyboard Notifications 这是键盘通知的代码

let center = NotificationCenter.default
center.addObserver(self,
                   selector: #selector(keyboardWillShow(_:)),
                   name: .UIKeyboardWillShow,
                   object: nil)

center.addObserver(self,
                   selector: #selector(keyboardWillHide(_:)),
                   name: .UIKeyboardWillHide,
                   object: nil)

I would suggest you use the inputView of the the textField to replace the keyboard with a date picker 我建议你使用textField的inputView用日期选择器替换键盘

dateTextField.inputView = datePicker

Like so 像这样

Or you can prevent the keyboard from showing up in the first place by using the 或者你可以通过使用防止键盘首先出现

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
    return false
}

You'll need to return false and add your custom code to show a datepicker before 您需要返回false并添加自定义代码以显示之前的日期选择器

Text field picker View use this 文本字段选择器视图使用此选项

    let pickerView = UIDatePicker (frame: CGRect(x: 0, y: 240, width:    self.view.frame.size.width, height: 250))
    pickerView.backgroundColor = UIColor.white
    pickerView.minuteInterval = 30
    textField.inputView = pickerView

    pickerView.addTarget(self, action: #selector(yourvc. datePickerChanged(sender:)), for: UIControlEvents.valueChanged)
    let toolbar = UIToolbar (frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 56))
    toolbar.barStyle = UIBarStyle.black
    toolbar.sizeToFit()

    var items = [UIBarButtonItem]()

    items.append(
        UIBarButtonItem(title: "Done".localized(lang:  self.language!), style: .plain, target: self, action: #selector(yourvc.pickerDoneClicked(sender:)))
    )
    toolbar.setItems(items, animated: true)
    toolbar.tintColor = UIColor.black
    textField.inputAccessoryView = toolbar


   func datePickerChanged(sender: UIDatePicker)
    {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "dd MMM yyyy"
    print("date picker --->%@",sender.date)
    }

  func pickerDoneClicked(sender: UIBarButtonItem)
   {
    textfield.resignFirstResponder()
    self.view.endEditing(true)
   }

1.Set the textField delegate like below image 1.设置textField委托,如下图所示

(A) (一种)

在此输入图像描述

(B) (B)

在此输入图像描述

2.Add textField delegate methods 2.添加textField委托方法

Swift 迅速

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
    // Implement your Date Time Picker initial Code here
  return false
}

Objective C 目标C.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
   // Implement your Date Time Picker initial Code here
    return NO;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM