Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
文本字段由iOS虚拟键盘覆盖_Ios_Objective C_Swift_Uitextfield - Fatal编程技术网

文本字段由iOS虚拟键盘覆盖

文本字段由iOS虚拟键盘覆盖,ios,objective-c,swift,uitextfield,Ios,Objective C,Swift,Uitextfield,嗨,我在单元格中有一个UItextField,当它开始编辑时,它被虚拟键盘覆盖。这是我制作的JSON表示,用于演示viewController中的组件 MyViewController : { UIView : { UIView : { height : 100 }, UITableView : { cell1 : myCell,

嗨,我在单元格中有一个UItextField,当它开始编辑时,它被虚拟键盘覆盖。这是我制作的JSON表示,用于演示viewController中的组件

MyViewController : {     
     UIView : {
          UIView : {
               height : 100
          },
          UITableView : {
               cell1 : myCell,
               cell2 : myCell,
               cell3 : myCell,
               cell4 : {
                            UITextField
                       }
          }
     }
}

当我开始编辑文本字段时,我应该怎么做才能使视图向上滚动?

您想在键盘即将显示时调整受影响视图的内容插入。然后在键盘关闭后将其重置为0。

标准做法是在键盘可见时使用滚动视图滚动视图内容。苹果在其文档中提供了示例代码:

清单5-3调整内容视图的框架并滚动 键盘上方的字段

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

    NSDictionary* info = [aNotification userInfo];

    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    CGRect bkgndRect = activeField.superview.frame;

    bkgndRect.size.height += kbSize.height;

    [activeField.superview setFrame:bkgndRect];

    [scrollView setContentOffset:CGPointMake(0.0, activeField.frame.origin.y-kbSize.height) animated:YES];
}

注册键盘通知还需要其他步骤,所有步骤都包含在上述文档中


文档中提到了其他方法,但没有一种是自动的。有时候,当这样的基本功能需要额外的代码时,比如这样,我们会被提醒,苹果的开发工具还有改进的余地。

首先,当键盘出现时,你需要从iOS获得通知

在viewDidLoad中,您可以将VC注册到observ:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWillShown:)
                                             name:UIKeyboardWillShowNotification object:nil];
现在您的键盘将显示:当键盘出现时,将调用方法。 在KeyboardWillShowed:方法中,您要更改tableViews内容插入集。这就是你要做的:

- (void)keyboardWillShown:(NSNotification*)aNotification {
    NSDictionary* info = [aNotification userInfo];
    self.keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey]CGRectValue].size;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0,self.keyboardSize.height, 0.0);
    self.tableView.contentInset = contentInsets;
    self.tableView.scrollIndicatorInsets = contentInsets;
    [self scrollToContent];
}
现在您更改了tableViews contentInset,并将键盘大小保存为属性keyboardSize,但未检查内容是否可见。在最后一行中,我调用了scrollToContent方法,它将tableView滚动到所需的位置:

-(void)scrollToShowActiveField {
    //your views size
    CGRect myViewRect = self.view.frame;
    //substract the keyboards size
    myViewRect.size.height -= self.kbSize.height;

    //offsettedFrame is your UITextView
    CGRect offsettedFrame = self.activeField.frame;

    //we want to see some "padding" between the textview and the keyboard so I add some padding
    offsettedFrame.origin.y += 100;

    if (!CGRectContainsPoint(myViewRect, offsettedFrame.origin) ) {
        [self.tableView scrollRectToVisible:offsettedFrame animated:YES];
    }
}

我假设您的UITableView具有全屏大小。如果没有,那么您需要根据需要调整contentInset的大小。

这不需要我的干预吗?它绝对需要您的干预。由于键盘是一个显示在视图上方的窗口,与应用程序不相交,因此由应用程序相应地移动视图。