我有一个观察到的方法,当显示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。
问题是您正在检查错误的帧:
UIKeyboardFrameBeginUserInfoKey
当显示键盘时,显示过程的开始处的框架高度对您而言并不重要。您想知道的是显示过程的end处的框架:
UIKeyboardFrameEndUserInfoKey
而且,您似乎已收到有关错误消息的通知。您尚未显示要注册的通知,但是方法名称keyboardWasShown
表示在显示键盘did时会通知您。为时已晚。此通知几乎没有任何意义。您想知道何时显示键盘will。