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

时间:2017-01-27 12:16:28

标签: ios swift swift3

如何在UITextField上使用DateTime选择器时隐藏键盘。当我点击UITextField时,首先显示一个键盘,而不是在选定的UITextField上显示DateTime Picker。 请给我适当的解决方案。

6 个答案:

答案 0 :(得分:0)

  1. 将textField委托设置为文本字段初始化的对象

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

  2. 实施文本字段委托方法

    (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{
    
    }
    
  3. 在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
    }
    

答案 1 :(得分:0)

UITextField 的以下委托方法中,您可以返回false,这样键盘就不会出现,您可以添加代码来显示您的日期选择器。

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

            //Your code to show datepicker

            return false
}

答案 2 :(得分:0)

您可以使用Keyboard notifications查找是否显示更好的键盘完成情况。当您发现键盘显示时,请在当前DateTime上显示textField选择器。

以下是键盘通知的代码

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

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

答案 3 :(得分:0)

我建议您使用textField的inputView将键盘替换为日期选择器

dateTextField.inputView = datePicker

喜欢这样

或者您可以使用

防止键盘首先显示出来
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
    return false
}

您需要返回false并添加自定义代码以在

之前显示日期选择器

答案 4 :(得分:0)

文本字段选择器查看使用此

    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)
   }

答案 5 :(得分:0)

1.设置textField委托,如下面的图像

(A)

enter image description here

(B)

enter image description here

2.添加textField委托方法

夫特

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

目标C

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