在 TPKeyboardAvoidingScrollView.m 中有一个方法(keyboardWillHide:),当键盘要隐藏时会调用它。在该方法中,滚动视图的内容偏移设置为 CGPointZero,因此您的滚动视图将到达第一个视图控制器。
- (void)keyboardWillHide:(NSNotification*)notification {
_keyboardRect = CGRectZero;
_keyboardVisible = NO;
// Restore dimensions to prior size
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationCurve:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]];
[UIView setAnimationDuration:[[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]];
self.contentInset = _priorInset;
//self.contentOffset = CGPointZero;//Replacing this with below line
self.contentOffset = CGPointMake(self.contentOffset.x, 0);
[self setScrollIndicatorInsets:self.contentInset];
_priorInsetSaved = NO;
[UIView commitAnimations];
}
在编辑文本字段时停止滚动-
- (void)keyboardDidShow:(NSNotification*)notification {
[self setScrollEnabled:NO];
//existing code
}
- (void)keyboardWillHide:(NSNotification*)notification {
[self setScrollEnabled:YES];
//existing code with modification of content offset
}
请记住,这可能会影响您使用 TPKeyboardAvoidingScrollView 对象的其他视图。