No longer able to hide keyboard during viewWillDis

2019-06-15 00:58发布

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.

7条回答
乱世女痞
2楼-- · 2019-06-15 01:06

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.

查看更多
放我归山
3楼-- · 2019-06-15 01:08

To hide the keyboard when the text field lost focus

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

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

    return YES;
}
查看更多
乱世女痞
4楼-- · 2019-06-15 01:12

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.

查看更多
Emotional °昔
5楼-- · 2019-06-15 01:12

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.

查看更多
ゆ 、 Hurt°
6楼-- · 2019-06-15 01:14

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.

查看更多
Lonely孤独者°
7楼-- · 2019-06-15 01:18

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.

查看更多
登录 后发表回答