我正在使用 this示例项目和我想要做的是,当您在 View 上放置一个 UITextField
时,当键盘下方的 UITextField
时, View 会向上移动一点。
我使用了 this 中的 TPKeyBoardAvoidingScrollView
类项目。但是,当我单击 UITextField
时, View 会向上移动,一切正常,但是当我单击背景时,它不仅会恢复正常屏幕大小并关闭键盘,还会返回到第一个 View 而不是坚持我们当时所处的观点。
还可以在弹出键盘时左右滚动,知道我该如何解决这个问题吗? Here是我将它们添加在一起的项目。
最佳答案
在 TPKeyboardAvoidingScrollView.m 中有一个方法 (keyboardWillHide:) 在键盘要隐藏时被调用。在该方法中, ScrollView 的内容偏移量设置为 CGPointZero,因此您的 ScrollView 将到达第一个 View Controller 。
- (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 对象的其他 View 。
关于ios - 使用键盘滑动 UIScrollView 并向上移动 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15290729/