在 swift 中,如何以编程方式隐藏光标并禁用 UITextfield 的剪切/粘贴

瓦希卜

当我单击 TextField 时,当我创建 UIExfield 的子类以隐藏复制/过去等按钮时,我遇到了问题。这是该类的代码:

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
    }

}

当我将它用于故事板中的 texfield 时,代码运行良好,但是当以编程方式制作 textField 时,我收到消息:线程 1:信号 SIGABRT

这是我的文本字段在我的视图控制器扩展中的一部分代码上的声明:

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
        
        

错误消息在我的 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() 
        }
}

谢谢。

亚姆瓦察尔帕特尔

将此方法添加到您的代码中并查看更改。您还可以自定义您可以对文本字段执行的操作。如果您希望禁用所有文本字段操作,则只需在 func canPerformAction 方法中“返回 false”即可。

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章