软键盘上的高度在多次触发时会发生变化

问题描述 投票:0回答:1

我有一个观察到的方法,当显示soft keyboard时会触发。它可以正常工作,但是由于某些原因,height中的soft keyboard会在隐藏后更改,然后再次显示。我找不到原因,并且隐藏委托中似乎没有任何更改其值的内容。是什么原因造成的?我已经解决了这个问题,方法是存储高度,然后第二次使用它,但是我想知道造成此问题的原因。

- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];

    CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    CGRect visibleRect = self.view.frame;

    if (_storedKeyboardHeight.size.height == 0) {
        _storedKeyboardHeight.size.height = keyboardSize.height;
    }

    visibleRect.size.height = _storedKeyboardHeight.size.height;
    visibleRect.origin.y = self.view.height - visibleRect.size.height;

    CGRect rectOfCellInTableView = [self.loginTableView rectForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];

    //This changes the second time
    NSLog(@"🦁 %f", keyboardSize.height);
    //so I store the original value here
    NSLog(@"🦁 %@", CGRectCreateDictionaryRepresentation(_storedKeyboardHeight));

    if ((rectOfCellInTableView.origin.y + rectOfCellInTableView.size.height) > visibleRect.origin.y){
        CGPoint scrollPoint = CGPointMake(0.0, (rectOfCellInTableView.origin.y + rectOfCellInTableView.size.height) - visibleRect.origin.y + 50);
        [self.loginTableView setContentOffset:scrollPoint animated:YES];
    }
}

第一次是291,第二次是233。

ios objective-c nsnotificationcenter iphone-softkeyboard
1个回答
0
投票

问题是您正在检查错误的帧:

UIKeyboardFrameBeginUserInfoKey

当显示键盘时,显示过程的开始处的框架高度对您而言并不重要。您想知道的是显示过程的end处的框架:

UIKeyboardFrameEndUserInfoKey

而且,您似乎已收到有关错误消息的通知。您尚未显示要注册的通知,但是方法名称keyboardWasShown表示在显示键盘did时会通知您。为时已晚。此通知几乎没有任何意义。您想知道何时显示键盘will

© www.soinside.com 2019 - 2024. All rights reserved.