15

The following code used to work in iOS6 to hide the keyboard when a view controller was popped off of the navigation stack:

- (void)viewWillDisappear:(BOOL)animated {
    [self.view endEditing:YES];
    [super viewWillDisappear:animated];
}

However, in iOS7, the [self.view endEditing:YES] line seems to get ignored. I tried the command in other view events (viewDidDisappear, viewWillAppear, and viewDidAppear), and the only one it worked in is viewDidAppear. It seems that once a "pop" is initiated, we lose the ability to hide the keyboard until the view controller is "pushed" back on the stack.

While placing the code in viewDidAppear does work to hide the keyboard, the bad thing is that the keyboard is displayed briefly when the viewController is pushed back on to the navigation stack...pretty unacceptable from a UI perspective.

Has anyone else had success in working around this issue? I would prefer not to have to write my own CANCEL button, but right now, that is the only thing I can think of that will work.

Alan Keele
  • 231
  • 3
  • 6
  • I do exactly what you have posted in `viewWillDisappear:` and it works for me. The keyboard goes away and the `UITextField` has its "end editing" delegate called. I just verified this on a device running iOS 7.0.4. All I did was tap the "back" button to pop the view controller. – rmaddy Dec 01 '13 at 23:50
  • I'm on 7.0.3. I'll try upgrading and see what happens. – Alan Keele Dec 02 '13 at 04:41
  • I truly doubt it's a 7.0.3/7.0.4 issue. Do you implement any delegate methods that might prevent a text field/view from resigning first responder? – rmaddy Dec 02 '13 at 04:43
  • 1
    You are right, it is not a 7.0.3/7.0.4 issue. I am implementing textFieldShouldBeginEditing, textFieldDidBeginEditing, and textFieldDidEndEditing methods. I commented those out and still had the same issue. You did give me an idea though. I checked the canResignFirstResponder property of the currently active text field just before and just after I issued a "pop" command. Right before the pop command, canResignFirstResponder was true. Right after the pop command, it was false. I haven't spotted anything in my code yet that could cause this, but I'll keep digging. – Alan Keele Dec 02 '13 at 05:28
  • Correction: canResignFirstResponder was true both before and after I issued the pop command. However, it became false in viewWillDisappear. – Alan Keele Dec 02 '13 at 05:38

7 Answers7

2

If it's a UITextView set the editable property to NO. I tried this and it hides the keyboard as soon as it's set. I haven't tried it with a UITextField but I'm guessing you'd get the same result with setting the enabled property to NO. If that doesn't work, create a UITextField with userInteractionEnabled set to NO as a background for a transparent UITextView and use the editable property as stated above.

Elliot Alderson
  • 546
  • 2
  • 17
  • Good answer. This is in fact the only thing that worked for me. set editable for the UITextView to 0 during textViewShouldEndEditing and then re-enable it when you push that view controller back onto the stack. – ecume des jours Mar 21 '15 at 21:01
1

There was a change in iOS 7 where view controllers that are presented as modal forms cannot dismiss the keyboard by default. To fix this, you need to override your view controller's disablesAutomaticKeyboardDismissal method and return NO.

Jim
  • 72,985
  • 14
  • 101
  • 108
  • I tried this without success. I added the following lines to my view controller, and the keyboard still stays visible – Alan Keele Dec 06 '13 at 20:02
  • (please ignore previous comment...got pulled away and ran out of the 5 minute window editing time) I tried this, but the keyboard still remained visible. I returned NO for disablesAutomaticKeyboardDismissal in my view controller, and for kicks, I even added it to the navigation controller and to the previous view controller in the navigation hierarchy. Upon checking, the canResignFirstResponder property for the current text field is still NO by the time viewWillDisappear kicks in, so it is still not letting go of the keyboard. – Alan Keele Dec 06 '13 at 20:18
  • this method not being called ios 7 – Jitendra May 20 '14 at 11:00
0

The problem is that somewhere between the time I press the "BACK" button and the time that viewWillDisappear fires, the current text field's canResignFirstResponder is getting set to FALSE, which is preventing the keyboard from hiding. I have not been able to discover anything in my code which could cause this, and I strongly suspect that it could be some kind of iOS 7 bug, as the same code worked for me under iOS 6.

As a workaround, I implemented the following solution. I subclassed UINavigationController and overrode the following method:

- (UIViewController *)popViewControllerAnimated:(BOOL)animated {
    [self.topViewController.view endEditing:YES];
    return [super popViewControllerAnimated:animated];
}

This caused the keyboard to appropriately disappear when I tapped the Back button to pop the current view controller. Nice sigh of relief that I didn't have to write a bunch of custom Back buttons.

Alan Keele
  • 231
  • 3
  • 6
  • 1
    Well on the second one it is obvious it will not reach `[self.topViewController.view endEditing:YES];` because you return before it, any code after a return will never see the light of day. And I wouldn't of said it was interesting you're basically overriding the method just to call it's super and do nothing else so it's like you never even did anything with it. – Popeye Dec 06 '13 at 17:02
  • Duh, now I feel sheepish! Well, I corrected the code (corrections in edited version above) and retested it with the same results. Calling endEditing AFTER running [super popViewControllerAnimated] resulted in the keyboard not hiding like it should. – Alan Keele Dec 06 '13 at 19:07
  • Nevermind....I have been staring at this way too long. That obviously would not work either. The topViewController would already have popped, so the endEditing would not be called on the controller I wanted anyways. I am re-editing the comment to take out the second part of my statement to avoid confusion. – Alan Keele Dec 06 '13 at 19:28
  • 1
    @AlanKeele Stumbled upon your question, I also wanted to dismiss the keyboard. Not sure if it will work for you but I just used [my_textfield resignFirstResponder] in the viewWillDisappear and it dismisses the keyboard every time. – denikov May 03 '14 at 11:07
0

To hide the keyboard when the text field lost focus

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{

  if ([textField isFirstResponder])
        [textField resignFirstResponder];

    return YES;
}
gdm
  • 7,647
  • 3
  • 41
  • 71
0

I tried a workaround. It may not be what you guys are expecting.

If you are using storyboard, You can resign the keyboard in "prepareForSeuge" method.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    [self.view endEditing:YES];
}

It worked fine for me.

Rajan Balana
  • 3,775
  • 25
  • 42
0

Approach given in the below will definitely hide the status bar in iOS7.

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

Add this to your .plist file (go to 'info' in your application settings)

View controller-based status bar appearance --- NO

Then you can call this line to hide the status bar:

[[UIApplication sharedApplication] setStatusBarHidden:YES];

In case your app is developed to support iPhone only, status bar won't hide when you run your app in iPad.

Luke
  • 11,426
  • 43
  • 60
  • 69
kart
  • 45
  • 11
-1

Put your UItextfield or UItextview for global declaration.

UITextfield textfield = your textfield object;

-(void)viewWillDisappear:(BOOL)animated 
{

    [self.view endEditing:YES];
    [textfield resignFirstResponder];
    [super viewWillDisappear:animated];
}
Rashad
  • 11,057
  • 4
  • 45
  • 73