Disable UITextField keyboard?
Asked Answered
S

10

62

I put a numeric keypad in my app for inputing numbers into a text view, but in order to input numbers I have to click on the text view. Once I do so, the regular keyboard comes up, which I don't want.

How can I disable the keyboard altogether? Any help is greatly appreciated.

Sop answered 11/4, 2011 at 0:51 Comment(0)
C
107

The UITextField's inputView property is nil by default, which means the standard keyboard gets displayed.

If you assign it a custom input view, or just a dummy view then the keyboard will not appear, but the blinking cursor will still appear:

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

If you want to hide both the keyboard and the blinking cursor then use this approach:

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    return NO;  // Hide both keyboard and blinking cursor.
}
Conjunctive answered 2/12, 2011 at 1:7 Comment(4)
Caleb's answer below explains this, but I wanted to add that any UIView can be set as the inputView. This will cause the view to animate in and animate out the same way the standard keyboard does.Pamulapan
This is useful when developing a custom keyboard app extension. If you include a UISearchBar or a UITextField in the custom keyboard, it will try to show the keyboard (although it IS IN a keyboard), so that means it will kill your current keyboard as of its views / controllers hierarchy and then recreate it. I solved it with the help with this old-school hack. Thank you!Verbatim
Finally I used a cleaner approach using the appearance proxy, but relying the same concept: [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setInputView:[UIView new]];Verbatim
This works but if you use Mac keyboard to write in UITextfield the app crashes. Its not a good solution.Hypochromia
D
74

For Swift 2.x, 3.x, 4.x, 5.x

textField.inputView = UIView()

does the trick

Darnell answered 17/12, 2015 at 21:57 Comment(3)
Martin and Satish did it again 👌🏻Ribonuclease
You can hide the accessoryView with textField.inputAccessoryView = UIView() and you can hide the cursor with textField.tintColor = .white as well (Swift 5.x).Juvenal
OutStanding working for me......Ventriloquism
Y
5

If it's a UITextField, you can set it's enabled property to NO.

If it's a UITextView, you can implement -textViewShouldBeginEditing: in its delegate to return NO, so that it'll never start editing. Or you can subclass it and override -canBecomeFirstResponder to return NO. Or you could take advantage of its editing behavior and put your numeric buttons into a view which you use as the text view's inputView. This is supposed to cause the buttons to be displayed when the text view is edited. That may or may not be what you want.

Yiyid answered 11/4, 2011 at 1:14 Comment(2)
its a text field btw. Well this is how i getting the buttons to write in the specific textfield, setting flags and calling if (current.tag == 10) { textfieldflag = 1; [loanAmount setUserInteractionEnabled:NO]; [period setUserInteractionEnabled:YES]; [interest setUserInteractionEnabled:YES]; //[deposit setUserInteractionEnabled:YES]; loanAmount.editable = NO; }Sop
wohoo, got it to work. Thanks for that, something so simple and google didnt have the answer.Sop
R
4

Depending on how you have your existing buttons working this could break them, but you could prevent the keyboard from showing up setting the textView's editable property to NO

myTextView.editable = NO
Riana answered 11/4, 2011 at 1:19 Comment(1)
He asked for UITextField, not UITextViewIntrust
C
2

I have the same problem when had 2 textfields on the same view. My purpose was to show a default keyboard for one textfield and hide for second and show instead a dropdown list.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField 

method simply did not work as I expected for 2 textfields , the only workaround I found was

    UIView* dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];    
    myTextField.inputView = dummyView; 
    myTextField.inputAccessoryView = dummyView; 
    myTextField.tintColor =  myTextField.backgroundColor; //to hide a blinking cursor

This will totally hide the keyboard for a target textField (DropDownList in my case) and show a default one when user switches to the 2nd textfield (Account number on my screenshot)

enter image description here

Cremate answered 25/7, 2016 at 17:53 Comment(1)
Your answer is really great! Because it has the solution with setting inputAccessoryView to dummyView. I was struggling- how to hide it from the bottom of the screen.Decalescence
C
1

enter image description hereThere is a simple hack to it. Place a empty button (No Text) above the keyboard and have a action Event assign to it. This will stop keyboard coming up and you can perform any action you want in the handle for the button click

Cloutier answered 19/10, 2019 at 18:1 Comment(0)
P
0

To disable UITextField keyboard:

  1. Go to Main.Storyboard
  2. Click on the UITextField to select it
  3. Show the Attributes inspector
  4. Uncheck the User Interaction Enabled

To disable UITextView keyboard:

  1. Go to Main.Storyboard
  2. Click on the UITextView to select it
  3. Show the Attributes inspector
  4. Uncheck the Editable Behavior
Proviso answered 14/7, 2016 at 20:35 Comment(1)
If you will do this textfield will be untouchable. So you cannot click and write any stuff.Semitropical
A
0

I used the keyboardWillShow Notification and textField.endEditing(true):

lazy var myTextField: UITextField = {
    let textField = UITextField()
    // ....
    return textField
}()

override func viewDidLoad() {
    super.viewDidLoad()

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
}

@objc func keyboardWillShow(_ notification: Notification) {
        
    myTextField.endEditing(true)

    // if using a textView >>> myTextView.endEditing(true) <<<
}
Archaeornis answered 22/8, 2020 at 6:37 Comment(0)
P
-2
private void TxtExpiry_EditingDidBegin(object sender, EventArgs e)
    {
        ((UITextField)sender).ResignFirstResponder();
    }

In C# this worked for me, I don't use the storyboard.

Paramount answered 28/10, 2016 at 13:7 Comment(0)
G
-3

In Xcode 8.2 you can do it easily by unchecking state "enabled" option.

  1. Click on the textField you want to be uneditable
  2. Go to attirube inspector on right side
  3. Uncheck "enabled" for State

enter image description here

Or if you want to do it via code. You can simply create an @IBOutlet of this text field, give it a name and then use this variable name in the viewDidLoad func (or any custom one if you intent to) like this (in swift 3.0.1):

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    myTextField.isEditable = false
}
Gambill answered 22/12, 2016 at 23:17 Comment(1)
isEditable doesn't existBracy

© 2022 - 2024 — McMap. All rights reserved.