Disable editing of a text field
There is one more way of avoiding keyboard appearing
You can set an UITapGestureRecognizer like this:
let tap = UITapGestureRecognizer(target: self, action: #selector(self.textFieldEditing))
self.enterSport.addGestureRecognizer(tap)
and
@objc func textFieldEditing() {
self.enterSport.resignFirstResponder()
}
With Swift 3 Apple changed the property userInteractionEnabled
to isUserInteractionEnabled
.
The code looks like this:
textfield.isUserInteractionEnabled = true
enterSport.userInteractionEnabled = false
instead of editing
.
editing
is a read-only property to indicate if the field is currently being edited or not.
Swift 5:
enterSport.isUserInteractionEnabled = false
To summarize the answers and comments above:
editing is a readonly property. You can't set it.
If it's a UITextView, it has an editable property which you can set to false.
If it's a UITextField, you need to use the enabled property or the userInteractionEnabled property. Either of those will prevent editing. As