Reputation: 93
I face a problem when a create a sub class of UITexfield to hide the copy/past etc... buttons when i click on the TextField. here is the code of the class:
class UITextFieldCustom: UITextField {
var enableLongPressActions = false
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!}
override init(frame: CGRect) {
super.init(frame: frame)}
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return enableLongPressActions
}
}
The code runs good when i use it for a texfield from a storyboard, but when the textField is made programaticaly, i receveive the message: Thread 1: signal SIGABRT
here is the declaration of my textField on a part of my code in an extension of my View Controller :
extension MenuViewController {
func tabBarCentre(nameTextField: UITextFieldCustom) {
let titleLeftbutton = UIButton(type: .system)
titleLeftbutton.tintColor = #colorLiteral(red: 0.370555222, green: 0.3705646992, blue: 0.3705595732, alpha: 1)
let leftButtonConfig = UIImage.SymbolConfiguration(
pointSize: 20,
weight: .bold,
scale: .small)
let leftButtonImage = UIImage(
systemName: "chevron.left",
withConfiguration: leftButtonConfig)
titleLeftbutton.setImage(leftButtonImage, for: .normal)
titleLeftbutton.addTarget(self, action: #selector(dateMoinsUn), for: .touchUpInside)
// Création du text central qui recevra le DatePicker
nameTextField.frame = CGRect(x: 0, y: 0, width: 70, height: 25)
nameTextField.backgroundColor = #colorLiteral(red: 0.9685223699, green: 0.9686879516, blue: 0.9685119987, alpha: 1)
nameTextField.placeholder = "Ajouter date"
nameTextField.font = nameTextField.font?.withSize(15)
nameTextField.font = UIFont.preferredFont(forTextStyle: .headline)
nameTextField.textAlignment = .center
the error message is in my viewController :
class MenuViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
tabBarCentre(nameTextField: titleNavBarTextField) // THE ERROR IS HERE !!!!
self.titleNavBarTextField.setInputViewDatePicker(target: self, textField: titleNavBarTextField, selector1: #selector(tapped), selector2: #selector(todayPressed))
titleNavBarTextField.text = displayToday()
}
}
thank you.
Upvotes: 1
Views: 694
Reputation: 36
Add this methods to your code and see the changes. You can also customize what you can do with your textfield. If you want all textfield actions to be disabled then just "return false" in func canPerformAction method.
override func caretRect(for position: UITextPosition) -> CGRect {
return CGRect.zero
}
override func selectionRects(for range: UITextRange) -> [UITextSelectionRect] {
return []
}
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(UIResponderStandardEditActions.cut(_:)) || action == #selector(UIResponderStandardEditActions.selectAll(_:)) || action == #selector(UIResponderStandardEditActions.paste(_:)) {
return false
}
// Default
return super.canPerformAction(action, withSender: sender)**
Upvotes: 2