I have a textfield which when tapped displays a UIPickerView
. I add it as inputView
of the text field. Sometimes when I tap the textfield, it displays keyboard instead of the picker view. I have added a custom class that subclasses UITextField
as well.
override func viewDidLoad() {
// ...
monthTextField.addTarget(self, action: #selector(monthTextFieldDidTap), for: UIControl.Event.touchDown)
}
@objc func monthTextFieldDidTap(textField: UITextField) {
activePickerType = PickerType.month
viewDatePicker(textField)
}
@objc func yearTextFieldDidTap(textField: UITextField) {
activePickerType = PickerType.year
viewDatePicker(textField)
}
func viewDatePicker(_ textField: UITextField) {
self.datePicker = UIPickerView.init(frame: CGRect.init(x: 0, y: 0, width: self.navigationController!.view.frame.size.width, height: 216))
self.datePicker.delegate = self
self.datePicker.dataSource = self
textField.inputView = self.datePicker
// ...
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if (textField == monthTextField) {
return true
}
return false
}
The custom subclass of UITextField
:
class PickerTextField: UITextField {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func caretRect(for position: UITextPosition) -> CGRect {
return CGRect.zero
}
func selectionRects(for range: UITextRange) -> [Any] {
return []
}
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
UIMenuController.shared.isMenuVisible = false
self.resignFirstResponder()
return false
}
}
How make sure that keyboard does not get displayed when tapped on the UITextField
and always display the UIPickerView
?
Place these lines in viewDidLoad instead of monthTextFieldDidTap
self.datePicker = UIPickerView.init(frame: CGRect.init(x: 0, y: 0, width: self.navigationController!.view.frame.size.width, height: 216))
self.datePicker.delegate = self
self.datePicker.dataSource = self
monthTextField.inputView = self.datePicker
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
if (textField == monthTextField) {
activePickerType = PickerType.month
viewDatePicker(textField)
}
return true
}