在iOS 11.3中未调用键盘通知

时间:2018-07-05 14:10:11

标签: ios objective-c notifications keyboard

我的应用程序中有键盘通知,并且在ios 10中工作正常,但是在ios11.3中未调用我的通知方法 下面是我的代码:

- (void)attach {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWasShown:(NSNotification*)aNotification {
    UIView* responder = [self findFirstResponder];
    if (responder) {
        _tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapAnywhere:)];
        [baseView addGestureRecognizer:_tapRecognizer];
        NSDictionary* info = [aNotification userInfo];
        CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
        CGRect screenRect = [RGLayout layout].fullScreen;
        CGRect frame      = responder.frame ;
    }
}

我试图找到解决方案,但我发现iOS11的唯一变化是UIKeyboardFrameEndUserInfoKey的键盘高度。

但是我的问题是,在ios11.3中未调用相同的代码,而我的keyboardWasShown在ios 10.2中工作

仅供参考:当用户单击下一个时,下一个文本字段将成为FirstResponder。

1 个答案:

答案 0 :(得分:0)

不知道为什么,但是行为很奇怪,通知正在一个屏幕而不是另一个屏幕中调用。

我已经通过以下代码解决了这个问题:

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (self.nextField) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.nextField becomeFirstResponder];
            [[NSNotificationCenter defaultCenter] postNotificationName: UIKeyboardDidShowNotification object:self];
        });
    }
    else{
        dispatch_async(dispatch_get_main_queue(), ^{
            [textField resignFirstResponder] ;
        });
    }
    return NO ;
}