iOS form sheet keyboard does not disappear

2019-07-02 07:21发布

问题:

I know that I have to call

- (BOOL)disablesAutomaticKeyboardDismissal {
    return NO;
}
//this dismiss keyboard on ios
- (BOOL) textViewShouldBeginEditing:(UITextView *)textView {
    [self.comments resignFirstResponder];
    return YES;
}

In order to dismiss keyboard on a form sheet, I also know that I have to call disablesAutomaticKeyboardDismissal in the navigation controller if I have one.

Problem is: I create forms sheet with navigation controller programmatically like:

UIStoryboard*  sb = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:[NSBundle mainBundle]];
SettingsViewController *settingsVC = [sb instantiateViewControllerWithIdentifier:@"settingsViewController"];
//add navigation controller 
UINavigationController *modalViewNavController= [[UINavigationController alloc] initWithRootViewController:settingsVC];
modalViewNavController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
modalViewNavController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:modalViewNavController animated:YES];

So I don't have a navigation controller class for form sheet, how can I dismiss keyboard in this case?

Do I have to create a navigation view controller?

UPDATE:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

End Up creating a navigation controller in the storyboard and call disablesAutomaticKeyboardDismissal in the navigation controller's view controller, that solved the problem but I still wonder how to do this without creating a navigation controller class.

Any input is welcomed......

回答1:

Can you try the following code;

- (void)viewDidLoad {
    [super viewDidLoad];

    UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
    gestureRecognizer.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:gestureRecognizer];
}

- (void)hideKeyboard {
    [self.view endEditing:YES];
}


回答2:

Below code worked for me:

@interface UINavigationController (dismissKeyBoard)

@end

@implementation UINavigationController (dismissKeyBoard)

- (BOOL)disablesAutomaticKeyboardDismissal
{
    return NO;
}

@end