【问题标题】:UIScrollView and keyboard iOSUIScrollView 和键盘 iOS
【发布时间】: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


【解决方案1】:
self.automaticallyAdjustsScrollViewInsets=NO;

根据苹果文档,automaticAdjustScrollViewInsets by 默认是。

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

将此添加到您的 viewdidload 方法中,然后尝试。

【讨论】:

    【解决方案2】:

    这是一种解决方案,

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

    https://github.com/michaeltyson/TPKeyboardAvoiding

    希望它对你有用。

    【讨论】:

    • 看看我的问题的更新...我不会在我的应用程序中使用任何外部库!
    【解决方案3】:

    我通过阅读此link in Apple documentation 解决了我的问题,所以在我的情况下,代码如下:

    - (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;
    }
    

    谢谢你回答我

    【讨论】:

      猜你喜欢
      • 2014-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-21
      • 2011-07-05
      • 2017-06-25
      • 2014-09-21
      相关资源
      最近更新 更多