【问题标题】:Disable UITextField editing but still accept touch - Swift 3禁用 UITextField 编辑但仍接受触摸 - Swift 3
【发布时间】:2023-04-01 09:06:01
【问题描述】:

我正在创建一个UITextField,它有一个UIDatePicker 作为输入。我不想编辑文本字段(复制、粘贴、剪切和选择文本),但我只想在用户触摸UITextField 时显示UIDatePicker,并且所选日期将显示在UITextField 上。

我尝试使用isUserInteractionEnabled = false 禁用编辑,但是当我触摸它时,TextField 没有给我响应。

我应该怎么做才能在 Swift 3 中实现它?

【问题讨论】:

  • 在TextField的委托中实现textFieldShouldBeginEditing(),在那里你可以调用UIDatePicker并返回false
  • 假设您在同一个视图中有其他文本字段,那么您可以使用 Tag 属性来识别已选择的文本字段,并对所有其他文本字段返回 true
  • UITextField 替换为UIButton 会解决您的问题吗?用户点击您的按钮,您显示日期选择器,然后一旦用户决定日期,您隐藏日期选择器。您可以设置 UIButton 的样式,使其看起来像 UITextField

标签: ios swift uitextfield uidatepicker


【解决方案1】:

为了防止出现复制/粘贴菜单……

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

并实施……

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
    // show UIDatePicker
    return false
}

这会在菜单出现之前隐藏菜单

【讨论】:

  • 对我来说效果很好。我不需要 Swift 3 中的 textFieldShouldBeingEditing 部分。
【解决方案2】:

使用 func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { rerun bool } 代替 textFieldShouldBeginEditing。

class ViewController: UIViewController , UITextFieldDelegate {

    @IBOutlet weak var textField: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()

        let datePicker = UIDatePicker()
        datePicker.datePickerMode = UIDatePickerMode.date
        textField.tag = 1
        textField.inputView = datePicker
    }

    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        if textField.tag == 1 {
            textField.text = ""
            return false
        }
        return true
    }
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if textField.tag == 1 {
            textField.text = ""
            return false
        }
        return true
    }
}

创建一个名为 StopPasteAction.swift 的新类

导入 UIKit

class StopPasteAction: UITextField {

    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        return false
    }
}

用你的当前文本字段添加类新类

【讨论】:

  • 对我不起作用。我仍然可以长按UITextField 并触发粘贴对话框。
  • @toddg 请检查保管箱上的代码dropbox.com/s/84wb7k23wdlkazc/UItextDelagte.zip?dl=0 如果工作请告诉我
  • 我不太需要它。 PS我不是那个否决这个答案的人。
【解决方案3】:

您可以addTargettextField,并在 textField 委托方法中禁用编辑,如下所示:

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var tedtField: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()

        tedtField.delegate = self
        tedtField.addTarget(self, action: #selector(respondsToTf), for: .touchDown)
    }

    func respondsToTf()  {

        // 
        print("you can pop-up the data picker here")
    }

    // MARK: - textfield delegate
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {

        return false
    }

}

【讨论】:

  • 可以工作,但必须自己显示和隐藏 datePicker
【解决方案4】:

迅速 3:

class MyViewController: UIViewController, UITextFieldDelegate {

   @IBOutlet weak var myTextField           : UITextField!

   override func viewDidLoad() {
      self.myTextField.delegate     = self
   }

   func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
      if textField == myTextField {
         // code which you want to execute when the user touch myTextField
      }
      return false
   }
}

【讨论】:

    【解决方案5】:

    我就是这样解决这个问题的

    class ViewController: UIViewController, UITextFieldDelegate {
    
    @IBOutlet weak var testTxf: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        testTxf.delegate = self
        let tap = UITapGestureRecognizer(target: self, action: #selector(testFunc))
        testTxf.addGestureRecognizer(tap)
        testTxf.isUserInteractionEnabled = true
    }
    
    @objc func testFunc() {
        print("tap")
    }
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        return false
    }
    

    }

    【讨论】:

      【解决方案6】:

      此外,您可以创建 UITextField 的子类并实现以下方法:

      import UIKit
      
      class MyCustomTextField: UITextField {
      
          override func caretRect(for position: UITextPosition) -> CGRect {
              return CGRect.zero
          }
      
          override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
      
              if action == #selector(copy) || action == #selector(cut) || action == #selector(paste){
                  return false
              }
              else {
                  return super.canPerformAction(action, withSender: sender)
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2012-02-25
        • 1970-01-01
        • 1970-01-01
        • 2020-03-30
        • 2014-10-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多