Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding the Keyboard on button click?

I am creating iPhone app which I show below.

At the end of screen I have text field. I have added a delegate for the same. As it is number pad, I have added button seperately so that when button is clicked, they keyboard is hidden.

Below is the code I have:

.h

@interface SearchViewController : UIViewController<UITextFieldDelegate>

@property (retain, nonatomic) IBOutlet UITextField *textField006;
@property (retain, nonatomic) IBOutlet UIButton *doneButton;
- (IBAction)doneAction:(id)sender;

.m

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    NSLog(@"textFieldShouldReturn");
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    NSLog(@"textFieldDidBeginEditing");
    // Ensure the relevant text field is visible
    CGAffineTransform translation = CGAffineTransformIdentity;
    CGRect screenBound = [[UIScreen mainScreen] bounds];
    CGSize screenSize = screenBound.size;
    CGFloat screenHeight = screenSize.height;

    if (screenHeight==480 || screenHeight==568) {
            translation = CGAffineTransformMakeTranslation(0, -120);
        doneButton.hidden = NO;
        NSLog(@"line 3");
        [UIView beginAnimations:nil context:nil];
        self.view.transform = translation;
        [UIView commitAnimations];
    }
}

- (IBAction)doneAction:(id)sender {
    doneButton.hidden = NO;    
    doneButton.hidden = YES;
    [textField006 resignFirstResponder];
    [UIView beginAnimations:nil context:nil];
    self.view.transform = CGAffineTransformIdentity;
    [UIView commitAnimations];
    [self.textField006 resignFirstResponder];

}

Why isn't the keyboard hiding? How can I hide it?

Keyboard == Decimal Pad Return key >> Go Auto-enable Return key = Ticked

like image 902
Fahim Parkar Avatar asked Mar 20 '13 10:03

Fahim Parkar


People also ask

How do I stop my keyboard from clicking on the button?

Android devices have a solution; press the physical back button (provided on some mobile phones) or the soft key back button, and it closes the keyboard.

How do you hide the keyboard?

To hide keyboard, use the following code. InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); inputMethodManager. hideSoftInputFromWindow(v. getApplicationWindowToken(),0);

How do I hide my keyboard when typing?

Tap the back button on your Android. It's the left-pointing arrow button at the bottom of the screen, either at the bottom-left or bottom-right corner. The keyboard is now hidden. The back button may be a physical button or on the touch screen. To bring the keyboard back into view, tap the typing area.


2 Answers

Be sure to use endEditing: if it's not currently hiding correctly.

About endEditing:

"endEditing causes the view (or one of its embedded text fields) to resign the first responder status."

"This method looks at the current view and its subview hierarchy for the text field that is currently the first responder. If it finds one, it asks that text field to resign as first responder. If the force parameter is set to YES, the text field is never even asked; it is forced to resign."

So, the following should work (inside your button click action method):

[self.view endEditing:YES];
like image 123
dsgriffin Avatar answered Oct 01 '22 04:10

dsgriffin


Swift Version of @lifetimes Answer

self.view.endEditing(true)

it worked perfectly for me

like image 29
Abhinav Jha Avatar answered Oct 01 '22 03:10

Abhinav Jha