I'm creating a UITextField that has a UIDatePicker as input. I don't want the textfield to be edited (copy, paste, cut and select text), but I only want to show UIDatePicker when users touch the UITextField and the chosen date will be displayed on UITextField.
I've tried to disable editing by using isUserInteractionEnabled = false, but the TextField doesn't give me response when I touch it.
What should I do to achieve it in Swift 3?
To prevent the copy/paste menu appearing…
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if dateTextField.isFirstResponder {
DispatchQueue.main.async(execute: {
(sender as? UIMenuController)?.setMenuVisible(false, animated: false)
})
return false
}
return super.canPerformAction(action, withSender: sender)
}
And implement…
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
// show UIDatePicker
return false
}
This hides the menu before it has a chance to appear
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With