Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable keyboard when clicking a UITextField in iOS?

I am currently developing a prototype that I want to do user testing on desktop first before loading to iPad.

I am looking for solutions to disable the keyboard after clicking a textfield. That means after clicking a textfield, user is able to enter information from the macbook keyboard directly, and the virtual keyboard that automatically shows up in the simulator will not appear. I have been through a lot of tutorials but they are all dismissing the keyboard after user entry; that is not what I am looking for. How should I hide the keyboard?

Thanks so much!

like image 422
user3185617 Avatar asked Jan 17 '14 20:01

user3185617


People also ask

Which method can be used to dismiss the system keyboard if a Uitextfield or Uitextview is currently being edited?

You can ask the system to dismiss the keyboard by calling the resignFirstResponder() method of your text field. Usually, you dismiss the keyboard in response to specific interactions. For example, you might dismiss the keyboard when the user taps the keyboard's return key.

How do I turn off TextField keyboard?

disable keyboard for Textfield coderanch.comsetEditable( false ); Sets the specified boolean to indicate whether or not this TextComponent should be editable. A PropertyChange event ("editable") is fired when the state is changed. (2) yourTextField. setEnabled( false ); Sets whether or not this component is enabled.

How to disable keyboard in TextField SwiftUI?

if you already have a TextField setup, you can add . disabled(true) , this will stop the keyboard from showing up.

How to dismiss keyboard in Swift Ui?

Pure SwiftUI (iOS 15) To dismiss the keyboard, simply set view's focusedField to nil . The return key will dismiss keyboard automatically (since iOS 14).


2 Answers

Use this:

UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];    
myTextField.inputView = dummyView; // Hide keyboard, but show blinking cursor

It works for UITextField and UITextView and they need to be set to editable.

What you did Here:
You created a dummy view of width=hight=0, & assigned it as the inputView of your textField.

How It works: Instead of showing default, keyboard, now, the viewController is showing DummyView as inputView for your UITextField. As DummyView has Width=height=0, You will not see anything on the screen :)

like image 91
Prince Agrawal Avatar answered Sep 28 '22 10:09

Prince Agrawal


Here is another answer which I found the same hack but with little additional supportive code snippet to hide the blinking cursor too.

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    return NO;  // Hides both keyboard and blinking cursor.
}

I needed this to be done for a Quantity text field where I increase/decrease the quantity using a UIStepper view. So I needed the keyboard to be hidden always.

like image 37
Randika Vishman Avatar answered Sep 28 '22 12:09

Randika Vishman