iOS form sheet keyboard does not disappear

2019-07-02 06:57发布

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......

2条回答
乱世女痞
2楼-- · 2019-07-02 07:26

Below code worked for me:

@interface UINavigationController (dismissKeyBoard)

@end

@implementation UINavigationController (dismissKeyBoard)

- (BOOL)disablesAutomaticKeyboardDismissal
{
    return NO;
}

@end
查看更多
老娘就宠你
3楼-- · 2019-07-02 07:39

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];
}
查看更多
登录 后发表回答