[英]Disable UITextField editing but still accept touch - Swift 3
我正在创建一个UITextField
,它有一个UIDatePicker
作为输入。 我不希望编辑文本字段(复制,粘贴,剪切和选择文本),但我只想在用户触摸UITextField
时显示UIDatePicker
,并且所选日期将显示在UITextField
。
我试图通过使用isUserInteractionEnabled = false
来禁用编辑,但是当我触摸它时TextField不会给我响应。
我应该怎么做才能在Swift 3中实现它?
防止出现复制/粘贴菜单...
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
}
这会在有机会出现之前隐藏菜单
使用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
}
}
使用Name StopPasteAction.swift创建一个新类
导入UIKit
class StopPasteAction: UITextField {
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return false
}
}
在Curren textfield中添加类新类
您可以将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
}
}
这就是我解决这个问题的方法
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
}
}
此外,您可以创建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)
}
}
}
迅捷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.