【发布时间】:2016-05-31 19:57:49
【问题描述】:
我正在为 iPad 开发一个应用程序,其中我在滚动视图视图中有一个表单 UITextField。
在这种形式中,我不止一个UITextField,就像这样:
所以当键盘关闭时,我使用以下方法点击“Via”UITextField:
- (void)keyboardWasShown:(NSNotification*)aNotification {
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
float bottom = 0.0;
bottom = kbSize.height;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, bottom + 50, 0.0);
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
CGRect aRect = self.view.frame;
aRect.size.height -= bottom + 50;
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y - (scrollView.frame.size.height - kbSize.width - 10));
[scrollView setContentOffset:scrollPoint animated:YES];
}
}
- (void)keyboardWillBeHidden:(NSNotification*)aNotification {
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
CGPoint scrollPoint = CGPointMake(0.0, 0.0);
[scrollView setContentOffset:scrollPoint animated:YES];
activeField = nil;
}
一切正常,滚动视图向上移动以显示我正在编辑的UITextField。
当打开键盘并点击“CAP”UITextField 时,滚动视图向上滚动,UITextField 在视图之外运行。
我在问你如何避免这种行为。
注意:我不会使用外部库来做到这一点!
谢谢
【问题讨论】:
-
我认为您必须尝试使用此代码来向上滚动视图 - (void)textFieldDidBeginEditing:(UITextField *)textField { CGPoint scrollPoint = CGPointMake(0, textField.frame.origin.y-120); [scrollView setContentOffset:scrollPoint 动画:YES]; } - (void)textFieldDidEndEditing:(UITextField *)textField { [scrollView setContentOffset:CGPointZero animated:YES]; }
-
你必须设置第一个 UItextField 的大小而不是 120
-
如果我的答案对你有用,那么告诉我,我可以发布它
标签: ios objective-c uiscrollview uitextfield uikeyboard