How to Disable Uitextfield Editing But Still Accept Touch

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"

Sample Image

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



Leave a reply



Submit