简体   繁体   中英

Keyboard getting displayed sometimes in textfield picker view in iOS

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

}

Add self.view.endEditing() line in monthTextFieldDidTap(textField: UITextField) function as below:

@objc func monthTextFieldDidTap(textField: UITextField) {
    self.view.endEditing(true)
    monthTextField.tintColor = UIColor.clear    //optional to use. It just removes cursor blinking on picker Textfield
    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
    // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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