【问题标题】:keeping content from underneath the keyboard保留键盘下方的内容
【发布时间】:2012-10-12 03:49:06
【问题描述】:

我有一个 UIViewController,我有一个 UIScrollView,里面有几个 UITextField。我从苹果那里得到了一段代码,它似乎从键盘下方移动了内容。

- (void)keyboardWasShown:(NSNotification *)notification
{
    // Step 1: Get the size of the keyboard.
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    // Step 2: Adjust the bottom content inset of your scroll view by the keyboard height.
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0);
    self.scrollView.contentInset = contentInsets;
    self.scrollView.scrollIndicatorInsets = contentInsets;
    // Step 3: Scroll the target text field into view.
    CGRect aRect = self.view.frame;
    aRect.size.height -= keyboardSize.height;
    if (!CGRectContainsPoint(aRect, self.firstResponder.frame.origin) ) {
        CGPoint scrollPoint = CGPointMake(0.0, self.firstResponder.frame.origin.y - (keyboardSize.height-10));
        [self.scrollView setContentOffset:scrollPoint animated:YES];
    }
}

我的 UIScrollView 是 320x416,因为我有一个导航栏。我的键盘也多了 44 个像素,因为我正在向它添加 UIToolBar,所以代码并没有真正工作。我尝试了各种配置来解决两个问题:

  1. 最后两个字段被 UIToolBar + 键盘覆盖,但只有最后一个触发 UIScrollView 移动。
  2. 除了 UIScrollView 在移动之外,由于 UIToolBar,UITextField 仍然在键盘后面。

更新:这行得通,但我很确定是错的,如果是对的,我不知道为什么,对我来说没有意义。有人可以修复/解释一下吗?

- (void)keyboardWasShown:(NSNotification *)notification
{
    // Step 1: Get the size of the keyboard.
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
    // Step 2: Adjust the bottom content inset of your scroll view by the keyboard height.
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height + 44.0f, 0.0);
    self.scrollView.contentInset = contentInsets;
    self.scrollView.scrollIndicatorInsets = contentInsets;
    // Step 3: Scroll the target text field into view.
    CGRect aRect = self.view.frame;
   aRect.size.height -= keyboardSize.height + 44.0f;
    if (!CGRectContainsPoint(aRect, self.firstResponder.frame.origin) ) {
        CGPoint scrollPoint = CGPointMake(0.0, self.firstResponder.frame.origin.y - (keyboardSize.height-10.0f - 44.0f - 44.0f - 44.0f));
        [self.scrollView setContentOffset:scrollPoint animated:YES];
    }
}

UPDATE1: 还有一个错误,我在 UIToolBar 中有一个上一个按钮,如果我点击最后一个文本字段,滚动视图会向上,如果我回到第一个文本视图,它在视图之外,因为滚动视图不会向下滚动。

【问题讨论】:

    标签: objective-c ios xcode cocoa-touch


    【解决方案1】:

    尝试将工具栏高度也添加到计算中,

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height + 44.0f, 0.0);
    

    也做,

    aRect.size.height = aRect.size.height - keyboardSize.height - 44.0f;
    

    【讨论】:

    • 第一行解决了1。但是第二行使UIScrollView下降而不是上升。
    • 忘记添加了。也改变这个,CGPoint scrollPoint = CGPointMake(0.0, self.firstResponder.frame.origin.y - (keyboardSize.height-10 + 44.0f));如果它仍然不起作用,请使用 aRect.size.height = aRect.size.height - keyboardSize.height - 44.0f 删除更改​​;部分。
    • 两种可能性都有问题,滚动视图正在下降。伙计,我尝试了很多东西。
    • 这一行,你可以改成这个吧?为什么要从滚动点减少键盘高度?检查这个。 CGPoint scrollPoint = CGPointMake(0.0, self.firstResponder.frame.origin.y);
    • 现在它可以工作了,但是文本字段会像前 0 一样一直向上。
    【解决方案2】:

    我尝试了与您在编辑部分相同的方法,但对 if 条件进行了轻微更改:

    !CGRectContainsRect(aRect, self.firstResponder.frame)
    

    这可能会帮助您解决仅原点位于键盘上方但文本字段被覆盖的问题。

    【讨论】:

      【解决方案3】:

      在尝试了很多解决问题的苹果解决方案后,我在描绘了苹果代码背后的一般想法后,最终自己编写了一些代码。顺便说一句,我认为苹果代码使问题复杂化并且实际上并不能正常工作。

      - (void)textFieldDidBeginEditing:(UITextField*)textField {
      
          self.firstResponder = textField;
      
          if (self.firstResponder.frame.origin.y + self.firstResponder.frame.size.height > self.view.bounds.size.height - (216 + 44)) {
      
              double fix = (self.firstResponder.frame.origin.y + self.firstResponder.frame.size.height) - (self.view.bounds.size.height - (216 + 44)) + 10;
      
              CGRect rect = CGRectMake(0, -fix, self.view.frame.size.width, self.view.frame.size.height);
      
              [UIView beginAnimations:nil context:NULL];
              [UIView setAnimationDuration:0.3];
              self.view.frame = rect;
              [UIView commitAnimations];
      
          } else if (self.firstResponder.frame.origin.y + 10 < -self.view.frame.origin.y) {
      
              double fix =  -self.view.frame.origin.y - (-self.view.frame.origin.y - self.firstResponder.frame.origin.y) - 10;
      
              CGRect rect = CGRectMake(0, -fix, self.view.frame.size.width, self.view.frame.size.height);
      
              [UIView beginAnimations:nil context:NULL];
              [UIView setAnimationDuration:0.3];
              self.view.frame = rect;
              [UIView commitAnimations];
      
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-11
        • 1970-01-01
        • 2016-06-16
        相关资源
        最近更新 更多