UIScrollView和键盘iOS

UIScrollView和键盘iOS,ios,objective-c,uiscrollview,uitextfield,uikeyboard,Ios,Objective C,Uiscrollview,Uitextfield,Uikeyboard,我正在为iPad开发一个应用程序,其中我在滚动视图中有一个表单,可以查看多个UITextField。 在这种形式中,我在一行中有一个UITextField,如下所示: 因此,当键盘关闭时,我用以下方法点击“Via”UITextField: - (void)keyboardWasShown:(NSNotification*)aNotification { NSDictionary* info = [aNotification userInfo]; CGSize kbSize =

我正在为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
在视图外向上运行。 我在问你如何避免这种行为

注意:我不会使用外部库来做这件事

多谢各位

self.automaticallyAdjustsScrollViewInsets=NO;
根据apple文档,AutomaticJustScrollViewInsets由 默认为是

所以它试图控制你的滚动视图

将此添加到viewdidload方法中,然后重试

这里有一个解决方案

如果您不想在scrollview中基于textField编写任何管理键盘的代码行,那么只需使用下面给出的第三方库,它将自动使用键盘管理您的textField


希望它能为你工作。

我通过阅读本文解决了我的问题,因此我的代码如下:

- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height + 50 , 0.0);
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;

    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your app might not need or want this behavior.
    CGRect aRect = self.view.frame;
    aRect.size.height -= kbSize.height;
    if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
        [self.scrollView scrollRectToVisible:activeField.frame animated:YES];
    }
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;
}

谢谢您的回答

我想您必须尝试使用此代码来启动scrollview-(void)TextFieldDidBeginEdit:(UITextField*)textField{CGPoint scrollPoint=CGPointMake(0,textField.frame.origin.y-120);[scrollview设置内容偏移:scrollPoint动画:是];}-(void)TextFieldDidEndEdit:(UITextField*)textField{[scrollView setContentOffset:CGPointZero动画:是];}您必须设置第一个UItextField大小,而不是120如果我的答案对您有用,请告诉我,这样我可以在更新我的问题时发布日志…我不会在我的应用程序中使用任何外部库!