Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No longer able to hide keyboard during viewWillDisappear in iOS7

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.

like image 835
Alan Keele Avatar asked Dec 01 '13 23:12

Alan Keele


1 Answers

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.

like image 78
Elliot Alderson Avatar answered Oct 19 '22 20:10

Elliot Alderson