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.

swiftBoy
  • 35,607
  • 26
  • 136
  • 135
michael
  • 621
  • 1
  • 5
  • 3

10 Answers10

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.
}
RohinNZ
  • 3,338
  • 5
  • 24
  • 34
  • 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. – BreadicalMD Jun 07 '13 at 15:43
  • 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! – CSolanaM Oct 22 '14 at 20:38
  • Finally I used a cleaner approach using the appearance proxy, but relying the same concept: [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setInputView:[UIView new]]; – CSolanaM Oct 22 '14 at 21:22
  • This works but if you use Mac keyboard to write in UITextfield the app crashes. Its not a good solution. – Norolim Mar 20 '17 at 14:51
74

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

textField.inputView = UIView()

does the trick

Martin Romañuk
  • 1,039
  • 10
  • 14
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.

Noah Witherspoon
  • 57,021
  • 16
  • 130
  • 131
Caleb
  • 124,013
  • 19
  • 183
  • 272
  • 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; } – michael Apr 11 '11 at 01:38
  • wohoo, got it to work. Thanks for that, something so simple and google didnt have the answer. – michael Apr 11 '11 at 01:40
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
jcane86
  • 681
  • 4
  • 17
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

David
  • 1,061
  • 11
  • 18
  • 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. – Vitya Shurapov Dec 25 '18 at 18:14
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

Nischal Revooru
  • 195
  • 2
  • 10
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
Abdulrazzaq Alzayed
  • 1,613
  • 1
  • 11
  • 15
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) <<<
}
Lance Samaria
  • 17,576
  • 18
  • 108
  • 256
-2
private void TxtExpiry_EditingDidBegin(object sender, EventArgs e)
    {
        ((UITextField)sender).ResignFirstResponder();
    }

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

User42
  • 970
  • 1
  • 16
  • 27
-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
}
Nabeel Khan
  • 3,715
  • 2
  • 24
  • 37