[英]Disable UITextField editing but still accept touch - Swift 3
I'm creating a UITextField
that has a UIDatePicker
as input. 我正在创建一个UITextField
,它有一个UIDatePicker
作为输入。 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
. 我不希望编辑文本字段(复制,粘贴,剪切和选择文本),但我只想在用户触摸UITextField
时显示UIDatePicker
,并且所选日期将显示在UITextField
。
I've tried to disable editing by using isUserInteractionEnabled = false
, but the TextField doesn't give me response when I touch it. 我试图通过使用isUserInteractionEnabled = false
来禁用编辑,但是当我触摸它时TextField不会给我响应。
What should I do to achieve it in Swift 3? 我应该怎么做才能在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 这会在有机会出现之前隐藏菜单
Use func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { retrun bool } in place of textFieldShouldBeginEditing. 使用func textField(_ textField:UITextField,shouldChangeCharactersIn范围:NSRange,replacementString string:String) - > Bool {retrun 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
}
}
Create a New class with Name StopPasteAction.swift 使用Name StopPasteAction.swift创建一个新类
import UIKit 导入UIKit
class StopPasteAction: UITextField {
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return false
}
}
Add the class new class with you Curren textfield 在Curren textfield中添加类新类
You can addTarget
to textField
, and in the textField delegate method, disable the editing like below: 您可以将addTarget
到textField
,并在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
}
}
That's how I solve this problem 这就是我解决这个问题的方法
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
}
} }
Additionally you can create a subclass of a UITextField and implement the methods below: 此外,您可以创建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)
}
}
}
swift 3: 迅捷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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.