How to disable keyboard appearing when hitting on a text field , iOS?
Asked Answered
M

8

13

I have a text field , and i need when the user presses it to show a custom picker.

The picker is shown fine , but the problem is that the keyboard appears on the bottom and i dont want that.

This is an iPad project which i am trying to convert from my iphone one. On the iPhone , this works well and the keyboard is always hidden.

What could i be missing/forgetting to do here ?

EDIT


For future reference what actually happened here , was that in fact both times (iphone & ipad) the keyboard was not hidden. I just thought that it was hidden in the iphone because my picker , which was popping from the bottom was hiding the keyboard as it was on top of it. But on ipad this wasnt the case.

Anyway i fixed it , using the delegate method suggested below.

Caution , i accepted this answer cause it was the one answering specifically what i wanted. The rest of the answers are correct and my considered better for other implementations.

Mortar answered 28/2, 2013 at 12:39 Comment(2)
does the keyboard show, even after setting the picker as a InputAccessoryView of the textField??Houseline
could you post your code?Opinionated
H
33
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    // Here You can do additional code or task instead of writing with keyboard
    return NO;
}

this delegate method will get called first when you hit to textfield and if you write NO as a boolean value means you dont want to begin editing so it will not present Keyboard.

Heavensent answered 28/2, 2013 at 14:20 Comment(0)
B
8
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if(textfield == yourtextField)
    {
        [textfield resignFirstResponder];
        // Show you custom picker here....
        return NO;
    }     
}

and you need to implement the uitextfielddelegate in the controller.

and give assign the delegate to yourtextField.

Bithia answered 28/2, 2013 at 12:48 Comment(0)
X
6

Use textfield delegate.

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{        
    return NO;
}
Xerosere answered 4/8, 2014 at 5:52 Comment(0)
S
3

It looks like all of these answers take one approach, to simply deny the keyboard before it comes up. This prevents first responder status, which has many advantages.

One simple approach that allows you to maintain first responder status is to create an empty view and assign that to the inputView property on your input field. If you are using iOS 9 (or later?) you will also have to get rid of the inputAssistantItem objects as well.

UITextField *field = [[UITextField alloc] init];
field.inputView = self.emptyKeyboard.view;

UITextInputAssistantItem *aItem = [field inputAssistantItem];
aItem.leadingBarButtonGroups = @[];
aItem.trailingBarButtonGroups = @[];

Then if you want to control the field from an alternate view controller, you can do so by adding targets:

[field addTarget:self.numberPad action:@selector(editingBegan:) forControlEvents:UIControlEventEditingDidBegin];
[field addTarget:self.numberPad action:@selector(editingEnded:) forControlEvents:UIControlEventEditingDidEnd];
[field addTarget:self.numberPad action:@selector(fieldChanged:) forControlEvents:UIControlEventEditingChanged];

It is also possible to do this a lot more cleanly by subclassing UITextField.

Sudden answered 18/2, 2016 at 4:2 Comment(0)
L
1

Use the textField Delegate,

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
 textField=nil;
 return NO;
}
Liegnitz answered 28/2, 2013 at 12:45 Comment(1)
Why you should set the textField to nil?Moisture
M
1

swift 3.0 version

First set the delegate for the text field

self.textfield.delegate = self

Then in an extension

extension ViewController: UITextFieldDelegate {
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        return false
    }
}
Microspore answered 7/7, 2017 at 15:53 Comment(0)
S
0
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    // Here you can do for Specific text Field by 
    if (textField==(the text field you don't want to show keyboard)) {
        NSLog(@"don't show keyboard");
        return NO;
    }
    else {
        return YES;
    }
}
Semiprofessional answered 24/7, 2014 at 9:31 Comment(0)
U
0

Swift 3/4

  1. Add:- UITextFieldDelegate in your class.

  2. Add:- self.textField.delegate = self In ViewDidLoad

  3. last one just add this func -

    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        return false
    }
    
Unbending answered 2/8, 2018 at 8:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.