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.
If it's a
UITextView
set theeditable
property toNO
. I tried this and it hides the keyboard as soon as it's set. I haven't tried it with aUITextField
but I'm guessing you'd get the same result with setting theenabled
property toNO
. If that doesn't work, create aUITextField
withuserInteractionEnabled
set toNO
as a background for a transparentUITextView
and use theeditable
property as stated above.To hide the keyboard when the text field lost focus
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 returnNO
.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.
It worked fine for me.
Approach given in the below will definitely hide the status bar in iOS7.
Add this to your .plist file (go to 'info' in your application settings)
Then you can call this line to hide the status bar:
In case your app is developed to support iPhone only, status bar won't hide when you run your app in iPad.
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:
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.