How to disable UITextField editing but still accept touch?
Using the textfield delegate, there's a method
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
Return NO from this, and any attempt by the user to edit the text will be rejected.
That way you can leave the field enabled but still prevent people pasting text into it.
Make textfield touch disabled but still accept editing?
Create a UITextField
subclass and override hitTest point
method and return nil
class CustomTextField: UITextField {
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
return nil
}
}
Use the custom class in the view controllers. In viewDidAppear
use becomeFirstResponder
in the first textfield, in textFieldShouldReturn
method move to next textfield. Make sure to set delegate to all textfields.
class ViewController: UIViewController, UITextFieldDelegate {
let textField1 = CustomTextField()
let textField2 = CustomTextField()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
textField1.delegate = self
textField1.returnKeyType = .next
view.addSubview(textField1)
textField2.delegate = self
textField2.returnKeyType = .send
view.addSubview(textField2)
//add constraints or set frame
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
textField1.becomeFirstResponder()
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == textField1 {
textField2.becomeFirstResponder()
}
return true
}
}
How to prevent editing of text in a UITextField without disabling other events using Swift 3?
So, I was finally able to get this working in the expected manner. What I did was -
1 - Extend the UITextFieldDelegate in the ViewController
class
class ViewController: UIViewController, UITextFieldDelegate {
2 - Inside the function viewDidLoad()
, assign the textfield delegate to the controller
override func viewDidLoad() {
super.viewDidLoad()
textfield.delegate = self
}
(you will have assign each UITextField's delegate that you want to be prevented from editing)
3 - Lastly, implement your custom behavior in the textFieldShouldBeginEditing
function
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return false
}
For reference, check out this API Reference on Apple Developer
Disabling user input for UITextfield in swift
Try this:
Swift 2.0:
textField.userInteractionEnabled = false
Swift 3.0:
textField.isUserInteractionEnabled = false
Or in storyboard uncheck "User Interaction Enabled"
How to disable UITextField's edit property?
To disable editing in UITextField you need to return NO
in delegate method textFieldShouldBeginEditing:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return NO;
}
To disable editing in UITextView set property editable
to NO
:
[textView setEditable:NO];
Related Topics
How to Get Image Metadata in iOS
How to Get Data from Firebase in Descending Order of Value
Xcode iOS Project Only Shows "My MAC 64-Bit" But Not Simulator or Device
Draw a Circle of 1000M Radius Around Users Location in Mkmapview
Send a File as Attachment in Objective C
How to Add an Action to a Button Programmatically in Xcode
iOS Touch Event Notifications (Private API)
What Are the Options for Saving Data in iOS
How to Disable/Remove Firebaseanalytics
Passing Parameters to a Method Called by Nstimer in Swift
Uiview - How to Get Notified When the View Is Loaded
How to Create a Simple Checkbox in iOS
Change String Color with Nsattributedstring
Ib_Designable, Ibinspectable -- Interface Builder Does Not Update