简体   繁体   中英

Prevent UITextField editing but detect taps

I have a UITextField and I am trying to detect the taps on it and prevent user typing input at the same time. The scenario is, I'm gonna use an alert view to make the user select an option and write it inside UITextField rather than user typing it using keyboard.

I tried using in viewDidLoad() :

myTextField.delegate = self
myTextField.isEnabled = true
myTextField.isUserInteractionEnabled = false

// for keyboard
myTextField.inputView = UIView()

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(didRecognizeTapGesture(_:)))
myTextField.addGestureRecognizer(tapGesture)

However this tap gesture doesn't work:

private dynamic func didRecognizeTapGesture(_ gesture: UITapGestureRecognizer) {
    print("YYYY")
    let point = gesture.location(in: gesture.view)
    guard gesture.state == .ended, taxableField.frame.contains(point) else { return }
    //doSomething()
}

Then I tried making userInteractionEnabled true and this time:

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
    if textField == myTextField {
        print("XXXX")
        return true
    }
    return true
}

This worked, however now I can long-press and paste into the textfield, so it is active, it's just not showing the keyboard this way.


So any of the ways I have tried don't seem convenient. What do you suggest for achieving such thing?

So, to create a UIPickerView and add it as your inputView, you need a delegate and a datasource. For this example, I'm going to assume that your VC will fill all of these roles. I usually split these out into custom classes, but I want to keep this simple:

// Data source for your picker view
var pickerViewData : [String]?
var myPickerView = UIPickerView()
@IBOutlet myTextField : UITextField!
func viewDidload() {
    myPickerView.delegate = self
    myPickerView.dataSource = self
    myTextField.delegate = self
    myTextField.inputView = pickerView
}

And then, to update your UITextField, whenever your picker view is used, implement the correct delegate method (It won't build unless you implement the method, regardless):

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    myTextField.text = pickerViewData![pickerView.selectedRow(inComponent: 0)]
}

And that's it! You also have to implement some other UIPickerView and UITextField delegate and datasource methods; I'm assuming you can figure out how to do this, if you don't know already.

You can take a UIButton over the UITextField and align its leading, trailing, top and Bottom with the UITextField .

Then add an action for the button.

Disable the TextField.

You can use UIPickerView to implement your function. If the picker must be a AlertView, you can replace the UITextField with a UILabel and put a UIButton upon it then you can implement the custom action in the UIButton callback and display the selection in the UILabel.

create a custom UIViewController which background color is clear and add a UIPickerView to show options. Show the UIViewController (overlay on top of current UIView) while UITextField is focus. So, you can do your handles within custom UIViewController.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM