【问题标题】:iOS - Move text field up not working alwaysiOS - 向上移动文本字段并不总是有效
【发布时间】:2017-04-14 10:26:26
【问题描述】:

我有一个表格视图和一个包含文本字段和多个按钮的视图。该视图位于屏幕底部,位于 UITableView 下方(作为标准消息面板)。当用户点击文本字段时,键盘出现,包括文本字段的视图向上移动。我还更改了表格视图的大小,缩短了它。一切都如我所愿。

但是,当我从这个视图控制器(用于查看图片)转到另一个视图控制器并返回时,该系统无法正常工作。键盘出现,但文本字段不上去。有时会有空白(因为有视图),但最后没有文本字段或按钮。

我的代码:

- (void) keyboardWasShown:(NSNotification *)notification {

    // get keyboard height
    NSDictionary* keyboardInfo = [notification userInfo];
    NSValue* keyboardFrameEnd = [keyboardInfo valueForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [keyboardFrameEnd CGRectValue];
    self.keyboardHeight = keyboardRect.size.height;

    // shorten tableview. so all of it will be visible
    [self.chatTableViewBottomConstraint setConstant:self.tableViewBottomToLayoutConstraint + self.keyboardHeight-54.0];
    [self.chatTableView setContentOffset:CGPointMake(0, CGFLOAT_MAX)]; // always the last item will be visible

    // move textfield up
    [self.textFieldViewBottomConstraint setConstant:+self.keyboardHeight-54.0];
    [self.view bringSubviewToFront:self.textFieldView];

}
- (void) keyboardWillBeHidden:(NSNotification *)notification {

    // restore tableview height and textfield position
    [self.textFieldViewBottomConstraint setConstant:0.0f];
    [self.chatTableViewBottomConstraint setConstant:self.tableViewBottomToLayoutConstraint];

}

在 UITableView 底部使用页脚对我的目标有好处,但是当表格视图为空时,此页脚会向上。这个问题不好。

那么,我错过了什么吗?为什么我的系统不能正常工作?

谢谢!

答案:问题不在于我的键盘方法。我找到了原因,它是关于我隐藏在其他视图控制器中的标签栏。我将它设置为不隐藏,一切正常。

换句话说,问题不在于键盘或文本字段。

【问题讨论】:

    标签: ios objective-c uitableview keyboard uitextfield


    【解决方案1】:

    使用下面的代码向上移动视图。

    - (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidEndEditing:) name:UIKeyboardDidHideNotification object:nil];
    
        [self.view endEditing:YES];
        return YES;
    }
    
      -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidStartEditing:) name:UIKeyboardDidShowNotification object:nil];
        return YES;
    }
    - (void)keyboardDidStartEditing:(NSNotification *)notification
    {
    
        [self.view setFrame:CGRectMake(0,-110,320,460)];
    
    }
    
    -(void)keyboardDidEndEditing:(NSNotification *)notification
    {
        [self.view setFrame:CGRectMake(0,0,320,460)];
    }
    

    希望它有效。

    【讨论】:

    • 它在转到另一个视图控制器后工作,但它的一般进度并不好。它在表格视图下方留下了奇怪的空白区域,例如在整个视图下方。我应该移动 textfieldview 而不是 self.view 吗?你怎么看?
    • 调整 CGRectMake 使您的视图正确地适合视图控制器。
    • 我当然做到了。仍然有很多问题。还是谢谢你。
    • 它没有。如果你检查,我编辑了我的问题。我的键盘方法完全有效。
    【解决方案2】:

    设置约束常量后,需要调用[self.view layoutIfNeeded]才能生效

    【讨论】:

      【解决方案3】:

      在您的keyboarWasShown 方法中,代码如下:

      - (void)keyboardWillShow:(NSNotification *)notification
      {
          NSDictionary *info = [notification userInfo];
      
          CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
          float kbHeight = kbSize.width > kbSize.height ? kbSize.height : kbSize.width;
      
          self.keyboardHeight.constant = kbHeight+5;
      }
      

      现在在UITextFieldtextFieldShouldBeginEditing委托方法中编写以下代码:

      - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
      {
      CGPoint point = [textField.superview convertPoint:textField.frame.origin toView:yourTableViewHere];
          NSIndexPath *indexPath = [tblAsset indexPathForRowAtPoint:point];
      
          CGRect tableViewVisibleRect = [tblAsset rectForRowAtIndexPath:indexPath];
      
          if (point.y > (tableViewVisibleRect.size.height - self.keyboardHeight.constant))
          {
      CGPoint contentOffsetPoint = tblAsset.contentOffset;
              contentOffsetPoint.y -= self.keyboardHeight.constant;
              CGSize contectSize = tblAsset.contentSize;
              contectSize.height += self.keyboardHeight.constant;
              [tblAsset setContentSize:contectSize];
              [tblAsset setContentOffset:contentOffsetPoint animated:NO];
          }
      }
      

      以上来自我当前的项目,它的工作方式有点像你想要的。 使用这种方式可以帮助您完成任务。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-20
        • 1970-01-01
        • 1970-01-01
        • 2016-07-07
        • 2020-08-16
        • 1970-01-01
        • 1970-01-01
        • 2020-04-14
        相关资源
        最近更新 更多